将调用哪个超级构造函数?如果我不在子类中调用超级构造函数,是否仍然会调用超级构造函数?
Which super constructor will be called? And is super constructor still invoked if i don't invoke a super constructor in subclass?
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
super(“faculty”);
}
}
class Employee extends Person {
private String name;
public Employee() {
name = “no name”;
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
name = s;
System.out.println(s);
}
}
class Person {
//What if there was a parameterized constructor here
// e.g. public Person(String s){
// ... code ...
// }
}
在上面的 Java 代码中,如果我将 Person class 留空,并在 Faculty class 的无参数构造函数中调用超级构造函数,则会调用 Employee 的构造函数。但是如果Personclass中有一个参数化的构造函数呢?将调用哪个超级构造函数?员工一还是人一?
如果我不在 subclass 中调用超级构造函数,是否仍然会调用超级构造函数?
如果您将参数化构造函数添加到 Person
,您的 Employee
class 将不会编译,因为默认情况下,将不再暗示无参数构造函数,但是您的 Employee
构造函数需要调用它。
现在,如果您的 Person
class 同时具有无参数和 String
参数化构造函数(具有相同的 Employee
实现),您的代码将编译,并且调用 Employee
的构造函数仍然会首先调用 Person
的无参数构造函数。
But what if there is a parameterized constructor in the Person class.
如果你这样做,你会得到一个很好的编译错误。
如果你的超级 class 构造函数有一个参数,你的子class 应该调用 super(arguments)
其中参数匹配参数。
如果超级 class 构造函数没有任何参数,您的 child class 将隐式调用 super()
。因此我们不必再次显式调用 super()
。
示例:
class GrandParent
{
public GrandParent(String s){
System.out.println("Calling my grandpa");
}
}
class Parent extends GrandParent
{
public Parent(){
super("");
System.out.println("Calling my pa");
}
}
class Child extends Parent
{
public Child(){
//super() implicitly invoked
System.out.println("Calling my child");
}
}
根据 运行 以下内容:
class Test
{
public static void main(String[] args){
new Child();
}
}
你得到:
Calling my grandpa
Calling my pa
Calling my child
以上输出回答了您后续的问题:
Employee one or Person one?
And is super constructor still invoked if i don't invoke a super constructor in subclass?
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
super(“faculty”);
}
}
class Employee extends Person {
private String name;
public Employee() {
name = “no name”;
System.out.println("(3) Employee's no-arg constructor is invoked");
}
public Employee(String s) {
name = s;
System.out.println(s);
}
}
class Person {
//What if there was a parameterized constructor here
// e.g. public Person(String s){
// ... code ...
// }
}
在上面的 Java 代码中,如果我将 Person class 留空,并在 Faculty class 的无参数构造函数中调用超级构造函数,则会调用 Employee 的构造函数。但是如果Personclass中有一个参数化的构造函数呢?将调用哪个超级构造函数?员工一还是人一?
如果我不在 subclass 中调用超级构造函数,是否仍然会调用超级构造函数?
如果您将参数化构造函数添加到 Person
,您的 Employee
class 将不会编译,因为默认情况下,将不再暗示无参数构造函数,但是您的 Employee
构造函数需要调用它。
现在,如果您的 Person
class 同时具有无参数和 String
参数化构造函数(具有相同的 Employee
实现),您的代码将编译,并且调用 Employee
的构造函数仍然会首先调用 Person
的无参数构造函数。
But what if there is a parameterized constructor in the Person class.
如果你这样做,你会得到一个很好的编译错误。
如果你的超级 class 构造函数有一个参数,你的子class 应该调用 super(arguments)
其中参数匹配参数。
如果超级 class 构造函数没有任何参数,您的 child class 将隐式调用 super()
。因此我们不必再次显式调用 super()
。
示例:
class GrandParent
{
public GrandParent(String s){
System.out.println("Calling my grandpa");
}
}
class Parent extends GrandParent
{
public Parent(){
super("");
System.out.println("Calling my pa");
}
}
class Child extends Parent
{
public Child(){
//super() implicitly invoked
System.out.println("Calling my child");
}
}
根据 运行 以下内容:
class Test
{
public static void main(String[] args){
new Child();
}
}
你得到:
Calling my grandpa
Calling my pa
Calling my child
以上输出回答了您后续的问题:
Employee one or Person one? And is super constructor still invoked if i don't invoke a super constructor in subclass?