方法调用的分派(重载与重写)

  • 静态分派(重载)

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); //hello guy!
        sr.sayHello(woman); //hello guy!
    }
}

对于 Human man = new Man();,Human称为变量的静态类型Man称为变量的实际类型
所有依赖静态类型来决定方法执行版本的分派动作,都称为静态分派。

重载方法优先级

class Test{
    public static void sayHello(Object arg) {
        System.out.println("hello Object");
    }
    public static void sayHello(int arg) {
        System.out.println("hello int");
    }
    public static void sayHello(long arg) {
        System.out.println("hello long");
    }
    public static void sayHello(Character arg) {
        System.out.println("hello Character");
    }
    public static void sayHello(char arg) {
        System.out.println("hello char");
    }
    public static void sayHello(char... arg) {
        System.out.println("hello char ...");
    }
    public static void sayHello(Serializable arg) {
        System.out.println("hello Serializable");
    }
    public static void main(String[] args) {
        sayHello('a'); //hello char
    }
}

上述代码自动转型顺序为char>int>long>float>double>Character>Serializable==Comparable >Object>char...,不会匹配到byte和short类型的重载。Serializable和Comparable(Character)是Character同时实现的两个接口,当其同时出现时,无法确定转型为哪种类型,提示“类型模糊”。Object为Character的父类,如果有多个父类,在继承关系中从下往上搜索。

  • 动态分派(重写)

在运行期根据实际类型确定方法执行版本的分派过程称为动态分派。

public class DynamicDispatch {
    static abstract class Human {
        protected abstract void sayHello();
    }

    static class Man extends Human {
        @Override
        protected void sayHello() {
            System.out.println("man say hello");
        }
    }

    static class Woman extends Human {
        @Override
        protected void sayHello() {
            System.out.println("woman say hello");
        }
    }

    public static void main(String[] args) {
        Human man = new Man();
        Human woman = new Woman();
        man.sayHello(); //man say hello
        woman.sayHello(); //woman say hello
        man = new Woman();
        man.sayHello(); //woman say hello
    }
}

通过实际类型找到对应的方法,如果没找到则按照继承关系从下往上依次寻找实际类型的父类。

字段没有多态性

public class FieldHasNoPolymorphic {
    static class Father {
        public int money = 1;
        public Father() {
            money = 2;
            showMeTheMoney();
        }
        public void showMeTheMoney() {
            System.out.println("I am Father, i have $" + money);
        }
    }
    static class Son extends Father {
        public int money = 3;
        public Son() {
            money = 4;
            showMeTheMoney();
        }
        public void showMeTheMoney() {
            System.out.println("I am Son,i have $" + money);
        }
        
    }
    public static void main(String[] args) {
        Father gay = new Son();
        System.out.println("This gay has $" + gay.money);
        //I am Son,i have $0
        //I am Son,i have $4
        //This gay has $2
    }
}

你可能感兴趣的:(方法调用的分派(重载与重写))