封装测试;呼叫继承的类的功能,但仍父类的功能被执行
公共类TestMain {
public static void main(String[] args) {
Animal cat = new Cat();
cat.xyz();
}
}
class Animal{
public static void xyz(){System.out.println("Inside animal");}
}
class Cat extends Animal{
public static void xyz(){System.out.println("Inside cat");}
}
输出:在内部猫类动物
[这里最好的xyz()应该已经执行,但它没有发生。相反,Animal类的xyz()正在执行。它只发生在我将这个xyz()函数设为静态时,否则就没有问题。 ] 请说明理由。
您的问题类似于[这个问题] [1 ]请检查我的[answer there] [2] [1]:http://stackoverflow.com/questi ons/27345510/inheritance-in-java-subclass/27345630 [2]:http://stackoverflow.com/questions/27345510/inheritance-in-java-subclass/27345630#27345630 – Nazgul 2014-12-08 05:29:55