【问题标题】:java inheritance problem - have to create empty constructor in father classjava继承问题 - 必须在父类中创建空构造函数
【发布时间】:2014-01-14 20:56:19
【问题描述】:

我在 netbeans ubuntu java 标准项目(测试准备)上编程。 当我创建 AccountStudent.java 时出现错误。

Account.java

public abstract class Account {
 protected double _sum;
 protected String _owner;
 protected static int accountCounter=0;

 public Account(String owner){
     this._sum=0;
     this._owner=owner;
     accountCounter++;
 }
}

AccountStudent.java- 错误:找不到符号:构造函数 Account()

public class AccountStudent extends Account{

}

修复问题 - 添加空 Account 构造函数:

Account.java

public abstract class Account {
 protected double _sum;
 protected String _owner;
 protected static int accountCounter=0;

 public Account(){

 }

 public Account(String owner){
     this._sum=0;
     this._owner=owner;
     accountCounter++;
 }
}

如果他已经存在,我为什么要创建空的构造函数 Account,因为他继承了 Object 类?

谢谢

【问题讨论】:

    标签: java inheritance


    【解决方案1】:

    为什么要创建空构造函数 帐户如果他已经存在,因为他 继承Object类?

    构造函数不会被继承。如果一个类没有显式构造函数,hte 编译器会默默地添加一个无参数默认构造函数,它除了调用超类无参数构造函数之外什么都不做。在您的情况下,AccountStudent 失败,因为Account 没有无参数构造函数。添加它是解决此问题的一种方法。另一种方法是向AccountStudent 添加一个构造函数,该构造函数调用Account 的现有构造函数,如下所示:

    public class AccountStudent extends Account{
        public AccountStudent(String owner){
            super(owner);
        }
    }
    

    【讨论】:

    • @Micheal:有错字。 public Account(String owner){super(owner);} 应该是 public AccountStudent(String owner){super(owner);}。
    【解决方案2】:

    Java 中的每个类都必须有一个构造函数,如果你没有定义一个,编译器会为你做这件事并创建一个默认构造函数(没有参数的那个)。如果你自己创建一个构造函数,那么编译器就不需要创建一个。

    所以即使它继承自 Object,也不意味着它会有一个默认的构造函数。

    当您实例化 AccountStudent 时,您需要调用父构造函数。 默认情况下,如果您不指定要调用的父构造函数,它将调用默认构造函数。 如果要显式调用父构造函数,可以使用super()

    有三种方法可以避免错误:

    使用从子构造函数获得的参数调用父构造函数:

    public class AccountStudent extends Account{
        public AccountStudent(String owner){
            super(String owner);
        }
    
    }
    

    使用您自己创建的参数调用父构造函数:

    public class AccountStudent extends Account{
        public AccountStudent(){
            super("Student");
        }
    
    }
    

    调用默认父构造函数,但您需要创建一个,因为如果非默认构造函数已经存在,编译器将不会创建一个。 (你给出的解决方案)

    【讨论】:

      【解决方案3】:

      JLS 8.8.9 Default Constructor

      如果类不包含构造函数声明,则自动提供不带参数的默认构造函数。 如果要声明的类是原始类 Object,则默认构造函数的主体为空。 否则,默认构造函数不带参数,只是简单地调用不带参数的超类构造函数。

      在这种情况下,AccountStudent 类没有任何构造函数,因此编译器会为您添加默认构造函数并添加对超类构造函数的调用。所以你的子类实际上看起来像:

      类 AccountStudent 扩展 Account{ 帐户学生(){ 极好的(); } }

      【讨论】:

        【解决方案4】:

        扩展类的对象包含状态变量(字段) 继承自超类以及定义的状态变量 在课堂上本地。构造一个扩展对象 类,您必须正确初始化两组状态变量。这 扩展类的构造函数可以处理自己的状态,但只有 超类知道如何正确初始化它的状态,这样它的 合同得到履行。扩展类的构造函数必须委托 通过隐式或显式构造继承状态 调用超类构造函数。

        Java™ 编程语言,第四版 作者:肯·阿诺德、詹姆斯·高斯林、大卫·福尔摩斯

        【讨论】:

        • 感谢您的回答。这个问题已经有两年多了,已经回答了。我们很高兴看到您在更紧迫的问题上付出的努力:)
        • 没问题,我试试!谢谢! :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 2021-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多