Home > Archives > JVM StaticDispatch

JVM StaticDispatch

Publish:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
    }
 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class StaticDispatch1 {
    static class Human {
        public void sayHello() {
            System.out.println("hello, guy!");
        }
    }

    static class Man extends Human {
        public void sayHello() {
            System.out.println("hello, gentleman!");
        }
    }

    static class Woman extends Human {
        public void sayHello() {
            System.out.println("hello, lady!");
        }
    }

    public static void main(String[] args) {
        Human human = new Human();
        human.sayHello();

        human = new Man();
        human.sayHello();

        human = new Woman();
        human.sayHello();
    }
}

声明: 本文采用 BY-NC-SA 授权。转载请注明转自: Ding Bao Guo