mirror of
https://github.com/fenixsoft/jvm_book.git
synced 2025-03-13 18:51:04 +08:00
38 lines
739 B
Java
38 lines
739 B
Java
package org.fenixsoft.jvm.chapter8;
|
|
|
|
/**
|
|
* 方法静态分派演示
|
|
* @author zzm
|
|
*/
|
|
public class StaticDispatch {
|
|
|
|
static abstract class Human {
|
|
}
|
|
|
|
static class Man extends Human {
|
|
}
|
|
|
|
static class Woman extends Human {
|
|
}
|
|
|
|
public void sayHello(Human guy) {
|
|
System.out.println("hello,guy!");
|
|
}
|
|
|
|
public void sayHello(Man guy) {
|
|
System.out.println("hello,gentleman!");
|
|
}
|
|
|
|
public void sayHello(Woman guy) {
|
|
System.out.println("hello,lady!");
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
Human man = new Man();
|
|
Human woman = new Woman();
|
|
StaticDispatch sr = new StaticDispatch();
|
|
sr.sayHello(man);
|
|
sr.sayHello(woman);
|
|
}
|
|
}
|