2017-04-19 66 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();