简体   繁体   English

在Java中继承构造函数

[英]Inheriting a constructor in java

When we use inheritance in java can we inherit constructor of the super class by the subclass? 当我们在Java中使用继承时,是否可以通过子类继承超类的构造函数?

At tutorialspoint it says: tutorialspoint上它说:

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. 子类从其超类继承所有成员(字段,方法和嵌套类)。 Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. 构造函数不是成员,因此它们不会被子类继承,但是可以从子类中调用超类的构造函数。

But in somewhere the same tutorial said that: 但是在同一教程的某处说:

If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. 如果一个类继承了另一个类的属性,则子类将自动获取超类的默认构造函数。

So I want to know can a subclass inherit the constructor of the super class? 所以我想知道子类可以继承超类的构造函数吗?

So I want to know can a subclass inherit the constructor of the super class? 所以我想知道子类可以继承超类的构造函数吗?

a constructor cannot be inherited. 构造函数不能被继承。

Moving on, I've managed to find the source which you're relating to at tutorialspoint . 继续,我设法在tutorialspoint找到与您相关的源。

It's important to note that the section under which it's mentioned is clearly titled as Invoking Superclass Constructor and it states: 重要的是要注意,在其下面提到的部分明确地标题为“ 调用超类构造函数” ,并且指出:

If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. 如果一个类继承了另一个类的属性,则子类将自动获取超类的默认构造函数。 But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below. 但是,如果要调用超类的参数化构造函数,则需要使用super关键字,如下所示。

 super(values); 

To put it simply, what this is trying to say is if the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass. 简单地说,这是要说的是,如果子类构造函数未指定要调用的超类构造函数,则编译器将自动调用超类中的可访问的无参数构造函数。

However, once there is a parameterized constructor within the super class you must invoke it as the first statement within the constructor of the derived class otherwise your program will not compile. 但是,一旦超类中存在参数化的构造函数,则必须将其作为派生类的构造函数中的第一条语句调用,否则您的程序将无法编译。

eg 例如

public class Super { ... }
public class Derived extends Super { ... } // good ! compiler puts in a public default constructor for you.

the code below will not compile and you should get an error stating: 下面的代码将无法编译,您应该得到一个错误说明:

there is no default constructor available in Super 在Super中没有可用的默认构造函数

public class Super {
    public Super(int id) {...} 
}
public class Derived extends Super {...}

meaning you'll need to invoke the Super class constructor explicitly as the first statement within the constructor of the Derived class, providing the necessary arguments, eg 这意味着您需要显式调用Super类的构造函数,作为Derived类的构造函数中的第一条语句,并提供必要的参数,例如

public Derived(){ super(12);}

You can access super class constructors with super keyword. 您可以使用super关键字访问超类构造函数。

You need to define the constructor in the sub class and invoke the super constructor. 您需要在子类中定义构造函数,然后调用超级构造函数。

public class Animal {

    private String name;

    public Animal(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void play(){
        System.out.println(this.getName() + " is playing ");
    }
}

public class Dog extends Animal{
    public Dog(String name) {
        super(name);
    }
}

public class Test {

    public static void main(String[] args) {
        Dog d = new Dog("Winky");
        d.play();
    }
}

All the super class constructors are inherited by default, you can call these constructors with the sub class constructor. 默认情况下,所有超类构造函数都是继承的,您可以使用子类构造函数调用这些构造函数。

When you instantiate sub class object, the sub class before sub class constructor expected it will execute the super class constructor and then it will continue to execute the sub class constructor. 当实例化子类对象时,子类构造函数之前的子类期望它将执行超类构造函数,然后它将继续执行子类构造函数。 If you would like to use other super constructor other than default super constructor you can call with super keyword from the sub class constructor and this call should be the first statement in the sub class constructor. 如果您想使用默认超级构造函数以外的其他超级构造函数,则可以从子类构造函数中使用super关键字进行调用,并且此调用应该是子类构造函数中的第一条语句。

example: 例:

public Subclass(String msg) {
    super(msg); // calling non default super class constructor
    // any other initialization
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM