2017-04-19 63 views
0

我們今天早上在java中討論了繼承問題,但是好像我的代碼有錯誤,我的教授無法幫助我,因爲他很忙,你能幫我指點我的錯誤嗎?繼承java「找不到符號」

package inheritance; 

class Inheritance { 

    void accelerate() 
    { 
    System.out.println("Drive"); 
    } 

    void turn() 
    { 
    System.out.println("Turn!"); 
    } 
} 


class n2wheels extends Inheritance { 


    void fast() 
    { 
    System.out.println("Swift Vehicle"); 
    } 

} 


class n4wheels extends Inheritance { 


    void normal() 
    { 
    System.out.println("Average Vehicle"); 
    } 


} 


class multiwheel extends Inheritance { 


    void slow() 
    { 
    System.out.println("Heavy Vehicle"); 


    } 


    public static void main(String[] args) 
    { 
    Inheritance try1 = new Inheritance(); 
    try1.normal(); 
    } 
} 
+0

您沒有一個名爲'no rmal'在你的類中稱爲'Inheritance'。 –

+0

'normal'不是繼承的方法。 – luk2302

+0

只是一個提示:關於Java中的繼承有很多解釋。看看這些並試圖瞭解它的含義以及它的工作原理。否則,你將來會遇到很多問題,因爲你顯然錯誤理解了那裏的某些東西。祝你好運! – Luftbaum

回答

0

正常()方法不存在繼承類

1

有一個在您Inheritance類沒有normal方法。

至少要做:

class Inheritance { 

    void accelerate() { 
     System.out.println("Drive"); 
    } 

    void turn() { 
     System.out.println("Turn!"); 
    } 

    void normal(){} 
} 

或:

n4wheels try1 = new n4wheels(); 
try1.normal(); 

爲側節點:請啓動大寫的類名稱。 N4Wheels,MultiWheels等...

0

您無法調用此方法,因爲它不存在於Inheritance類中並存在於n4wheels類中。我不知道你在做什麼,所以我在下面給你提供可能的解決方案。

解決方案1:如果您想在Inheritance類中調用normal(),則只需在同一個類中聲明它。

class Inheritance { 

    void accelerate() { 
    System.out.println("Drive"); 
    } 

    void turn() { 
    System.out.println("Turn!"); 
    } 

    void normal() { 
    // do something 
    } 
} 

解決方案2:如果你想打電話n4wheels類的正常的()方法直接,那麼:

n4wheels try1 = new n4wheels(); 
try1.normal(); 

解決方案3:如果你想那麼執行多態,你必須如'解決方案1'中所述,在Inheritance類中聲明normal()方法,然後,

Inheritance try1 = new n4wheels(); 
try1.normal();