2015-12-21 76 views
-3
public abstract class Pokemon{ 
private String _Name; 
private String _Status; 
private double _Hitpoint; 
private int _Level; 
private String _Type="General"; 
protected Pokemon(){ 
_Status="Alive"; 
_Hitpoint=50; 
_Level=5; 
} 

protected Pokemon(double Hitpoint, int Lvl){ 
_Status="Alive"; 
_Name="Pokemon"; 
_Hitpoint=Hitpoint; 
_Level=Lvl; 
} 

public String getStatus(){ 
return Status 
} 
public String getName(){ 
return _Name; 
} 

public double getHitpoint(){ 
return _Hitpoint; 
} 

public int getLevel(){ 
return _Level; 
} 

public abstract void makeNoise(); 

public abstract void dealDamage(); 
} 

以上是抽象父類。下面是子類:Java繼承:未定義方法

public class Pikachu extends Pokemon{ 
String _Type="Electric Pokemon"; 
String _Name; 
public Pikachu(){ 
this("Pikachu",50,5); 
} 

public Pikachu(String Name,double _Hitpoint,int _Level){ 
super(_Hitpoint,_Level); 
_Name=Name; 
} 



public void makeNoise(){ 
    System.out.println("Pika Pika"); 
} 

public void dealDamage(){ 
    System.out.println(getName()+" uses Shockwave"); 
} 
public static void main(String args[]){ 
    Pikachu Pika=Pikachu(); 
    Pika.makeNoise(); 
    Pika.dealDamage(); 
} 
} 

當我試圖運行代碼,在該行發生錯誤:

Pikachu Pika=Pikachu(); 
Error: The method Pikachu() is undefined for the type Pikachu 

很抱歉,如果這個問題很業餘(我剛開始學習使用Java)。任何人都可以給我一個簡單的解釋,爲什麼我的皮卡丘是不確定的,而我以前實際上已經定義了皮卡丘方法?

謝謝

+7

'皮卡丘鼠兔=新皮卡丘();'(注意新的'new')。 – Maroun

+1

閱讀關於如何使用構造函數。在理解之前嘗試繼承是沒有用的 – Stultuske

回答

3

構造需要與new

Pikachu Pika = new Pikachu();

0

你需要創建皮卡丘的新實例調用,用new關鍵字。

Pikachu Pika = new Pikachu(); 

更多here