有如下程序:
public class Animal{
public void eat(){
System.out.println("Generic Animal Eating Generically");
}
}
public class Horse extends Animal{
public void eat(){
System.out.println("Horse eating hay");
}
public void eat(String s){
System.out.println("Horse eating " + s );
}
}
第一个:Animal ah = new Horse(); ah.eat();
它的运行结果是:Horse eating hay
第二个:Animal ah2 = new Horse(); ah2.eat("Carrots");
它的运行结果是:编译器出错!
第一个的原因有标注说是多态性起的作用,而第二个则是说编译器仍只是看到了该引用类型,
有谁能解释下具体原因不,通俗易懂的.....
public class Animal{
public void eat(){
System.out.println("Generic Animal Eating Generically");
}
}
public class Horse extends Animal{
public void eat(){
System.out.println("Horse eating hay");
}
public void eat(String s){
System.out.println("Horse eating " + s );
}
}
第一个:Animal ah = new Horse(); ah.eat();
它的运行结果是:Horse eating hay
第二个:Animal ah2 = new Horse(); ah2.eat("Carrots");
它的运行结果是:编译器出错!
第一个的原因有标注说是多态性起的作用,而第二个则是说编译器仍只是看到了该引用类型,
有谁能解释下具体原因不,通俗易懂的.....