2010-08-17 62 views
1

我在netbeans ubuntu java standart项目(测试准备)上编写程序。 当我创建AccountStudent.java我得到错误。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 - 错误:无法找到符号:构造帐户()

public class AccountStudent extends Account{ 

} 

修正了problem-加空账构造:

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++; 
} 
} 

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

感谢

回答

8

Why should i create empty constructor Account if already he exist because he inherit Object class?

构造函数不能被继承。如果一个类没有显式构造函数,hte编译器会默默地添加一个无参数的默认构造函数,除了调用超类无参数构造函数外,它什么也不做。在你的情况下,AccountStudent失败,因为Account没有无参数的构造函数。添加它是解决这个问题的一种方法。另一个办法是一个构造函数添加到AccountStudent调用的Account现有的构造函数,像这样:

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

这错误应该被改写,以在有意义 – TheLQ 2010-08-17 23:29:17

+0

@Micheal:有错字错误。公共帐户(字符串所有者){超级(所有者);}应该是公共AccountStudent(字符串所有者){超级(所有者);}。 – Shashi 2010-08-18 15:08:34

+0

@Shashi:谢谢,更正 – 2010-08-18 16:06:16

3

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"); 
    } 

} 

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

1

JLS 8.8.9 Default Constructor

如果一个类不包含任何构造函数声明,则将自动提供一个默认的构造函数没有参数。 如果声明的类是原始类Object,那么默认的构造函数有一个空的主体。 否则,默认的构造函数不接受任何参数,只调用没有参数的超类构造函数。

在这种情况下,AccountStudent类没有任何构造函数,因此编译器为您添加了一个默认构造函数,并且还向超类构造函数添加了一个调用。所以,你的子类有效的样子:

 
    class AccountStudent extends Account{ 
     AccountStudent() { 
     super(); 
    } 
    } 
1

An object of an extended class contains state variables (fields) that are inherited from the superclass as well as state variables defined locally within the class. To construct an object of the extended class, you must correctly initialize both sets of state variables. The extended class's constructor can deal with its own state but only the superclass knows how to correctly initialize its state such that its contract is honored. The extended class's constructors must delegate construction of the inherited state by either implicitly or explicitly invoking a superclass constructor.

Java™编程语言,第四版 由肯·阿诺德,詹姆斯·高斯林,大卫·霍姆斯

+0

感谢您的回答。这个问题已经过了两年多了,已经回答了。我们很高兴看到你在更紧迫的问题上的努力:) – Mifeet 2014-01-14 13:21:31

+0

没问题,我试试!谢谢! :) – MarkHunt 2014-01-14 13:34:18