2010-08-17 21 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