Java中的默认构造函数和继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/525548/
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
Default constructors and inheritance in Java
提问by user42155
I have a question about default constructors and inheritance in Java.
我有一个关于 Java 中默认构造函数和继承的问题。
Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null, or false). If you write a constructor, however, with some parameters, and you don't write any default constructor, then Java does not provide a default constructor. My question is: what is the case with classes, which inherit from other classes - if I write a constructor with some parameters in them, but don't include a default constructor, do they inherit the default constructor of the super class?
一般来说,如果你写一个类并且不包含任何构造函数,Java会自动为你提供一个默认构造函数(一个没有参数的),它用一些默认值(0、null)初始化类的所有实例变量(如果有的话) ,或假)。但是,如果您编写带有一些参数的构造函数,并且没有编写任何默认构造函数,则 Java 不提供默认构造函数。我的问题是:从其他类继承的类是什么情况 - 如果我编写一个带有一些参数的构造函数,但不包含默认构造函数,它们是否继承了超类的默认构造函数?
回答by starblue
Constructors are not inherited.
构造函数不是继承的。
Also, the initialization of fields is done by the virtual machine, not the default constructor. The default constructor just invokes the default constructor of the superclass, and the default constructor of Object is empty. The good point of this design is that there is no way to ever access uninitialized fields.
此外,字段的初始化由虚拟机完成,而不是默认构造函数。默认构造函数只是调用超类的默认构造函数,Object的默认构造函数为空。这种设计的好处是无法访问未初始化的字段。
回答by Peter Lawrey
Unless you use super(...) a constructor calls the empty constructor of its parent. Note: It does this on all you classes, even the ones which extend Object.
除非您使用 super(...) 构造函数调用其父的空构造函数。注意:它对你所有的类都这样做,即使是那些扩展 Object.
This is not inheriting, the subclasses don't get the same constructors with the same arguments. However, you can add constructors which call one of the constructors of the super class.
这不是继承,子类不会获得具有相同参数的相同构造函数。但是,您可以添加调用超类的构造函数之一的构造函数。
回答by potyl
If you provide a constructor then Java will not generate you a default empty constructor. So your derived class will only be able to call your constructor.
如果您提供构造函数,则 Java 不会为您生成默认的空构造函数。所以你的派生类只能调用你的构造函数。
The default constructor doesn't initialize your private variables to default values. The proof is that it's possible to write a class that doesn't have a default constructor and has its private members initialized to default values. Here's an example:
默认构造函数不会将您的私有变量初始化为默认值。证明是可以编写一个没有默认构造函数并且将其私有成员初始化为默认值的类。下面是一个例子:
public class Test {
public String s;
public int i;
public Test(String s, int i) {
this.s = s;
this.i = i;
}
public Test(boolean b) {
// Empty on purpose!
}
public String toString() {
return "Test (s = " + this.s + ", i = " + this.i + ")";
}
public static void main (String [] args) {
Test test_empty = new Test(true);
Test test_full = new Test("string", 42);
System.out.println("Test empty:" + test_empty);
System.out.println("Test full:" + test_full);
}
}
回答by paulmurray
If you do not make a constructor, the default empty constructor is automatically created.
If any constructor does not explicitly call a super or this constructor as its first statement, a call to super() is automatically added.
如果不创建构造函数,则会自动创建默认的空构造函数。
如果任何构造函数未显式调用 super 或 this 构造函数作为其第一条语句,则会自动添加对 super() 的调用。
Always.
总是。
回答by Thusitha
When we don't create a constructor Java creates a default constructor automatically. But when we create one or more custom constructors with arguments, Java doesn't create any default constructors. If we create one or more constructors and we want to create an object without any constructor arguments, we have to declare a empty constructor.
当我们不创建构造函数时,Java 会自动创建一个默认构造函数。但是当我们创建一个或多个带参数的自定义构造函数时,Java 不会创建任何默认构造函数。如果我们创建一个或多个构造函数,并且想创建一个没有任何构造函数参数的对象,我们必须声明一个空的构造函数。
回答by Muthu N
Thumb Rule is that Sub Class should call any constructor from the base class. so if you don't have the default const the call the existing one from sub class. other wise implement the empty const in the base class to avoid the compilation problem
Thumb 规则是子类应该从基类调用任何构造函数。所以如果你没有默认的 const 调用子类中的现有常量。否则在基类中实现空常量以避免编译问题
回答by Abhishek Nair
The basic rule is a call (or invocation) to a constructor should be the first statement that JVM needs to execute,
基本规则是对构造函数的调用(或调用)应该是 JVM 需要执行的第一条语句,
So when you have a super class with only parameterized constructor and no default constructor, and base class has no explicit call to the parameterized constructor of the super class, JVM provides the super(); call which throws error as there is no default constructor for the super class, so either we provide a default constructor in the super class or we explicitly call the parameterized constructor of the super class in the base class constructor. when we give the explicit call, then JVM doesn't bother to put the line super(); as constructor invocation should be the first statement of the method, which cannot happen (because of our explicit call).
所以当你有一个只有参数化构造函数而没有默认构造函数的超类,并且基类没有显式调用超类的参数化构造函数时,JVM 提供了 super(); 由于超类没有默认构造函数,因此调用会引发错误,因此我们要么在超类中提供默认构造函数,要么在基类构造函数中显式调用超类的参数化构造函数。当我们给出显式调用时,JVM 不会费心放置 super(); 因为构造函数调用应该是方法的第一条语句,这不会发生(因为我们的显式调用)。
回答by dasblinkenlight
Section 8.8.9 of the Java Language Specification explains in details what is going on:
Java 语言规范的第 8.8.9 节详细解释了正在发生的事情:
If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructorfor a top level class, member class, or local class is as follows:
- The default constructor has the same accessibility as the class (§6.6).
- The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).
- The default constructor has no throws clauses.
- If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.
如果类不包含构造函数声明,则隐式声明默认构造函数。顶级类、成员类或局部类的默认构造函数的形式如下:
- 默认构造函数具有与类相同的可访问性(第 6.6 节)。
- 默认构造函数没有形式参数,除非在非私有内部成员类中,默认构造函数隐式声明一个形式参数,表示该类的直接封闭实例(第 8.8.1 节、第 15.9.2 节、第 15.9.3 节) )。
- 默认构造函数没有 throws 子句。
- 如果声明的类是原始类 Object,则默认构造函数的主体为空。否则,默认构造函数只调用不带参数的超类构造函数。
You can see that there is no inheritance going on here: all there is to it is the "compiler magic" with implicitly declared default constructor. The specification also makes it clear that the default constructor is added only when the class has no constructors at all, meaning that the answer to your question is "no": once you give a class a constructor, the access to the default constructor of its superclass is lost.
你可以看到这里没有继承:所有的都是带有隐式声明的默认构造函数的“编译器魔法”。该规范还明确指出,仅当类根本没有构造函数时才添加默认构造函数,这意味着您的问题的答案是“否”:一旦您为类提供了一个构造函数,就可以访问其默认构造函数超类丢失了。
回答by Ashish
The answer to your question is very simple. Implicitly(Invisible), the first statement in any constructor is 'super();' i.e. the call to super class's no parameter constructor, until you change it explicitly to something like 'this();','this(int)','this(String)','super(int)','super(String)' etc. 'this();' is current class's constructor.
你的问题的答案很简单。隐式(Invisible),任何构造函数中的第一条语句是“super();” 即调用超类的无参数构造函数,直到您将其显式更改为类似 'this();','this(int)','this(String)','super(int)','super(String) )' 等 'this();' 是当前类的构造函数。
回答by Shubham Soni
There will be compile time error...because Compiler looks for default Constructor he Superclass and if its not there...its an error...and program will not Compile...
会有编译时错误......因为编译器会寻找他超类的默认构造函数,如果它不存在......它会出现错误......并且程序将不会编译......