class Animal{}
class Horse extends Animal{
}
public class TestOverride {
public static void doStuff(Animal a){
System.out.println("In the animal case");
}
public static void doStuff(Horse a){
System.out.println("In the animal case");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Animal a = new Horse();
doStuff(a);
}
}
输出:
In the animal case
选择哪个重载方法是方法的签名决定的,并不是在与运行时决定的
本文探讨了Java中方法重载的选择原则,解释了如何基于方法签名来确定调用哪个重载方法,而不是依赖于运行时类型。通过具体示例说明了这一概念。
561

被折叠的 条评论
为什么被折叠?



