package com.generator;
public class TestOverload {
void sayHello(Human human) {
System.out.println("Human");
}
void sayHello(Man human) {
System.out.println("man");
}
void sayHello(Woman human) {
System.out.println("Woman");
}
public static void main(String[] args) {
Man man = new Man();
Human man2 = new Man();
Woman woman = new Woman();
Human woman2 = new Woman();
TestOverload testOver = new TestOverload();
testOver.sayHello(man);
testOver.sayHello(man2);
testOver.sayHello(woman);
testOver.sayHello(woman2);
}
}
class Human {
}
class Man extends Human{
}
class Woman extends Human{
}
class Child extends Human{
}
运行结果:
man
Human
Woman
Human
结论:
虚拟机在重载时是通过参数的静态类型而不是实际类型判断重载方法的。Human man = new Man()
中Human
是静态类型,Man
是实际类型
网友评论