我知道我在这里问一些严重的101个问题
我有一些类Foo和一个扩展Foo的类Bar 。 在Foo我有一个构造函数,它将一组参数设置为其字段。 派生类(例如Bar通常不需要修改这个。 现在我的IDE给了我“Foo中没有默认的构造函数” 。 来自谷歌的一些这似乎是因为“构造函数不被继承”。 所以很好,很好,但是如何在每个派生类中重复这个构造函数呢? 我假设有一个更健全的方法?
I know I'm asking some serious 101 question here...
I have some class Foo and a class Bar that extends Foo. In Foo I have a constructor that takes a set of parameters that it sets to its fields. Deriving classes such as Bar will typically not need to modify this. Now my IDE is giving me "There is no default constructor available in Foo". From a bit of Googling this appears to be because "constructors are not inherited". So all nice and well, but how do I now get this to work without duplicating this constructor in every deriving class? I'm assuming there is a more sane approach?
最满意答案
使用super构造函数:
public Bar(int a, double b, ...) { super(a, b, ...); }Use the super constructor:
public Bar(int a, double b, ...) { super(a, b, ...); }更多推荐
发布评论