我只是想知道如何使用子類來獲得超類的實現,例如。如何使用上傳或其他方法從子類實現超類?
class Animal {
void poo() {
System.out.println("general poo");
}
}
class Horse extends Animal{
void poo() {
System.out.println("horse poo");
}
}
Animal animal1 = new Horse();
// using this I get the implementation of the Horse class's methods
animal1.poo(); //should return horse poo
試圖上溯造型它來獲得超類的實現,但無濟於事
((Animal)animal1).poo() // still returns the horse class's implementation of the method
我如何使用animal1對象的超類實現?
這將是可能的,如果它是C#。 –
這是不可能的。請參閱http://stackoverflow.com/questions/2432249/accessing-super-class-function-using-subclass-object – Addict