Java 如何隐式调用父类的构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18436076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implictly call parent class's constructor
提问by Linghua Jin
I want to do following thing:
我想做以下事情:
class P {
P(int a) {
// construct
}
}
class C extends P {
}
// in main
int a = 2;
C foo = new C(a); // can I do this?
I want create child object C by calling parent class P's constructor without writing any constructor in class C like "super(a)". Is that possible?
我想通过调用父类 P 的构造函数来创建子对象 C,而无需在类 C 中编写任何构造函数,例如“super(a)”。那可能吗?
The idea is that I have a lot of class like "class C" which needs the same constructor functionality as "class P". So I don't want write a constructor method each time I create a new similar class.
这个想法是我有很多像“C 类”这样的类,它们需要与“P 类”相同的构造函数功能。所以我不想每次创建一个新的类似类时都写一个构造函数方法。
Thanks
谢谢
采纳答案by Ted Hopp
Constructors are not inherited. You will need to declare a constructor in C
that takes an argument. It will need to invoke the appropriate superclass constructor (if you don't want the default superclass constructor to be called).
构造函数不是继承的。您将需要声明一个C
带有参数的构造函数。它将需要调用适当的超类构造函数(如果您不想调用默认的超类构造函数)。
The only constructor you can avoid declaring is the default, no-arg constructor, and that only if you declare no constructors. That's because the compiler will generate one for you if you have no constructors declared. The compiler always inserts a call to the default superclass constructor unless you explicitly call a superclass constructor.
您可以避免声明的唯一构造函数是默认的无参数构造函数,并且仅当您不声明构造函数时才可以。那是因为如果您没有声明构造函数,编译器将为您生成一个。除非您显式调用超类构造函数,否则编译器始终插入对默认超类构造函数的调用。
Note also that if you do not explicitly call a superclass constructor and there is no default constructor in the superclass, you will get a compile-time error.
另请注意,如果您没有显式调用超类构造函数并且超类中没有默认构造函数,则会出现编译时错误。
EDIT: If you have "lots of classes like class C", then an alternative to writing a lot of constructors is to use a factory method pattern. You can have all the classes implement a default constructor and provide a separate init()
method that takes a standard set of arguments. The factory method would accept a Class<? extends C>
object and some initialization arguments, generate a new instance (using the default constructor), and call its init
method with the initialization arguments. That way you only need to override the init
method for those subclasses that need special handling (always remembering to call through to super.init
).
编辑:如果你有“很多类,比如 C 类”,那么编写大量构造函数的另一种方法是使用工厂方法模式。您可以让所有类实现一个默认构造函数并提供一个单独的init()
方法,该方法采用一组标准参数。工厂方法将接受一个Class<? extends C>
对象和一些初始化参数,生成一个新实例(使用默认构造函数),并init
使用初始化参数调用其方法。这样你只需要覆盖init
那些需要特殊处理的子类的方法(总是记得调用 to super.init
)。
回答by Anirudha
- A constructor implicitly calls the parameter-less constructor of it's immediate super class(only if there's no explicit call)
- When you define your own constructor,the default constructor would not be created.
- 构造函数隐式调用其直接超类的无参数构造函数(仅当没有显式调用时)
- 当您定义自己的构造函数时,不会创建默认构造函数。
So,in your case Class C
has a default constructor which would try to implicitly call the default constructor of Class P
which doesn't exits and would fail.
因此,在您的情况下 ClassC
有一个默认构造函数,它会尝试隐式调用 Class 的默认构造函数,P
该构造函数不会退出并且会失败。
So,you have to do it this way
所以,你必须这样做
class P
{
public P(int a)
{
// construct
}
}
class C extends P
{
public C(int x)
{
super(x);
}
}