java静态分派

静态分派发生在编译时期,分派根据静态类型信息发生。方法重载就是静态分派

 

public class BlackHorse implements Horse{

}

 

public class BlackHorse implements Horse{

}

 

public class WhiteHorse implements Horse {

}

 

public class Mozi {

    public void ride(Horse h){
        System.out.println("Riding a horse");
    }
    public void ride(WhiteHorse h){
        System.out.println("Riding a white horse");
    }
    public void ride(BlackHorse h){
        System.out.println("Riding a black horse");
    }
}

 

public class Client {

    public static void main(String[] args) {
        Horse horse = new BlackHorse();
        Horse horse2 = new WhiteHorse();
        
        Mozi mozi = new Mozi();
        mozi.ride(horse);
        mozi.ride(horse2);
    }
}

  运行结果:

Riding a horse
Riding a horse

 

你可能感兴趣的:(java)