简体   繁体   English

Java子类的构造函数

[英]constructor of subclass in Java

When compiling this program, I get error-编译此程序时,出现错误-

 class Person {
    Person(int a) { }
 }
 class Employee extends Person {
    Employee(int b) { }
 }
 public class A1{
    public static void main(String[] args){ }
 }

Error- Cannot find Constructor Person().错误 - 找不到构造函数 Person()。 Why defining Person() is necessary?为什么需要定义 Person() ?

When creating an Employee you're creating a Person at the same time.创建Employee您同时创建了一个Person To make sure the Person is properly constructed, the compiler adds an implicit call to super() in the Employee constructor:为了确保Person被正确构造,编译器在Employee构造函数中添加了对super()的隐式调用:

 class Employee extends Person {
     Employee(int id) {
         super();          // implicitly added by the compiler.
     }
 }

Since Person does not have a no-argument constructor this fails.由于Person没有无参数构造函数,因此失败。

You solve it by either你解决它

  • adding an explicit call to super, like this:添加对 super 的显式调用,如下所示:

     class Employee extends Person { Employee(int id) { super(id); } }
  • or by adding a no-arg constructor to Person :或者通过向Person添加一个无参数构造函数:

     class Person { Person() { } Person(int a) { } }

Usually a no-arg constructor is also implicitly added by the compiler.通常,编译器也会隐式添加无参数构造函数。 As Binyamin Sharet points out in the comments however, this is only the case if no constructor is specified at all.然而,正如 Binyamin Sharet 在评论中指出的那样,只有在根本没有指定构造函数的情况下才会出现这种情况。 In your case, you have specified a Person constructor, thus no implicit constructor is created.在你的情况,你已经指定了一个人的构造,因此没有隐式构造函数创建。

The constructor for Employee doesn't know how to construct the super-class, Person . Employee的构造函数不知道如何构造超类Person Without you explicitly telling it, it will default to trying the no-args constructor of the super-class, which doesn't exist.如果没有您明确告诉它,它将默认尝试不存在的超类的无参数构造函数。 Hence the error.因此错误。

The fix is:修复方法是:

class Employee extends person {
    public Employee(int id) {
        super(id);
    }
}

Java actually views this code as: Java实际上将此代码视为:

class Person {
  Person(int nm) { }
 }
 class Employee extends Person {
    Employee(int id) {
        super();
    }
 }
 public class EmployeeTest1 {
    public static void main(String[] args){ }
 }

Since there is no Person() constructor this fails.由于没有 Person() 构造函数,因此失败。 Instead try:而是尝试:

class Person {
  Person(int nm) { }
 }
 class Employee extends Person {
    Employee(int id) {
        super(id);
    }
 }
 public class EmployeeTest1 {
    public static void main(String[] args){ }
 }

Java provides you with a default constructor which takes no parameters. Java 为您提供了一个不带参数的默认构造函数。 The constructor also has no body, so it is something like so: public Person() {} .构造函数也没有主体,所以它是这样的: public Person() {} The moment you define you own constructor, your constructor(s) take place of the default one, so in your case, Person(int nm){} takes place of Person() { } .在您定义自己的构造函数的那一刻,您的构造函数将取代默认的构造函数,因此在您的情况下, Person(int nm){}取代Person() { } Your call is trying to implicitly call Person() { } and since this constructor no longer exists, the code is failing.您的调用试图隐式调用Person() { }并且由于此构造函数不再存在,因此代码失败。 Take a look at this previous SO question for more information.查看这个以前的 SO 问题以获取更多信息。

Above answer is correct, some addition: you can straight call to super(int n) to avoid implicitly adding super() here:上面的答案是正确的,还有一些补充:您可以直接调用 super(int n) 以避免在此处隐式添加 super() :

class Employee extends Person {
    Employee(int id) { super(int id);}
}
  1. If you write no constructor, compiler implicitly add default (without parameters) to your class.如果您不编写构造函数,编译器会向您的类隐式添加默认值(不带参数)。
  2. If you write any constructor - compiler will not add default constructor.如果您编写任何构造函数 - 编译器将不会添加默认构造函数。

PS:Sorry for my writing. PS:对不起我的写作。 English is not my native language.英语不是我的母语。

This is not 100% related your problem.这不是 100% 与您的问题相关。 But this is also related to java constructors.但这也与java构造函数有关。 Let's say your Person class has no default constructor and the constructor parameters are not primitive data type.假设您的 Person 类没有默认构造函数,并且构造函数参数不是原始数据类型。 They are objects.它们是对象。

But If you want to create a subclass with a default constructor, here is the solution.但是如果你想创建一个带有默认构造函数的子类,这里是解决方案。 And keep it mind you are not allowed to change the super class.请记住,您不得更改超类。

I created a another class called contact.我创建了另一个名为 contact 的类。

class Contact{
    private String name;
    private int number;

    public Contact(String name, int number) {
        this.name = name;
        this.number = number;
    }
}

Then here is the code.然后这里是代码。

//Super Class   -  "Can't change the code"
class Person {
    Person(Contact contact) { }
}

//Sub Class
class Employee extends Person {
    private static Contact c;

    static {
        c = new Contact("Anna",10);
    }

    Employee() {
        super(c);
    }
}

You have to static variable of Contact class to keep a object instance for passing to super class constructor.您必须使用 Contact 类的静态变量来保留对象实例以传递给超类构造函数。

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

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