Java语言静态多分派 与 动态单分派

publicclass Cat {

    publicvoid beFeeded(Person p){

       p.feed(this);

    }

}

publicclass WhiteCat extends Cat {

    publicvoid beFeeded(Person p){

       p.feed(this);

    }

}

publicclass BlackCat extends Cat {

    publicvoid beFeeded(Person p){

       p.feed(this);

    }

}

publicclass Person {

    publicvoid feed(Cat cat) {

       System.out.println("Person feedcat");

    }

 

    publicvoid feed(WhiteCat cat) {

       System.out.println("Person feedWhiteCat");

    }

 

    publicvoid feed(BlackCat cat) {

       System.out.println("Person feedBlackCat");

    }

 

}

publicclass Man extends Person {

    publicvoid feed(Cat cat) {

       System.out.println("Man feedcat");

    }

 

    publicvoid feed(WhiteCat cat) {

       System.out.println("Man feedWhiteCat");

    }

 

    publicvoid feed(BlackCat cat) {

       System.out.println("Man feedBlackCat");

    }

 

}

publicclass Woman extends Person {

    publicvoid feed(Cat cat) {

       System.out.println("Woman feedcat");

    }

 

    publicvoid feed(WhiteCat cat) {

       System.out.println("Woman feedWhiteCat");

    }

 

    publicvoid feed(BlackCat cat) {

       System.out.println("Woman feedBlackCat");

    }

}

publicclass Client {

//  一个方法包括三部分:方法的接受者、方法名、方法的参数签名(包括参数类型、参数个数、参数顺序)。

//  其中方法的接受者与方法签名称为方法的宗量

     publicstaticvoid main(String[]args) {

        test1();//说明动态分派

        test2();//说明动态单分派

        test3();//使用反转球实现两次动态单分派

        }

     publicstaticvoid test1(){

        Cat cat = new Cat();//可用下面两条语句替代,对结果无影响

//     Cat cat = new BlackCat();

//     Cat cat = new WhiteCat();

        Person p;

        

        p = new Person();

        p.feed(cat);

        

         p = new Man();

         p.feed(cat);

        

         p = new Woman();

         p.feed(cat);

//       Java语言调用期发生了向下转型,就是动态分派了。也就是常说的多态

//               运行结果:

//       Person feed cat

//       Man feed cat

//       Woman feed cat

     }

     publicstaticvoid test2(){

        Cat cat = new Cat();

        Cat bc = new BlackCat();

        Cat wc = new WhiteCat();

        

        Person p;

        

        p = new Person();

        p.feed(cat);

        p.feed(bc);

        p.feed(wc);

        

         p = new Man();

         p.feed(cat);

        p.feed(bc);

        p.feed(wc);

        

         p = new Woman();

         p.feed(cat);

        p.feed(bc);

        p.feed(wc);

//       不论参数为Cat的任何子类型,都不会被考虑,因此Java语言只考虑接受者这个宗量,因此是动态单分派

//               运行结果:

//     Person feed cat

//     Person feed cat

//     Person feed cat

//     Man feed cat

//     Man feed cat

//     Man feed cat

//     Woman feed cat

//     Woman feed cat

//     Woman feed cat

     }

     publicstaticvoid test3(){

        Cat cat = new Cat();

        Cat blackCat = new BlackCat();

        Cat whiteCat = new WhiteCat();

        

        Person p = new Person();

        Person man = new Man();

        Person woman = new Woman();

        

        cat.beFeeded(p);

        blackCat.beFeeded(p);

        whiteCat.beFeeded(p);

        

        cat.beFeeded(man);

        blackCat.beFeeded(man);

        whiteCat.beFeeded(man);

        

        cat.beFeeded(woman);

        blackCat.beFeeded(woman);

        whiteCat.beFeeded(woman);

//       出发两次动态单分派

//     Person feed cat

//     Person feed BlackCat

//     Person feed WhiteCat

//     Man feed cat

//     Man feed BlackCat

//     Man feed WhiteCat

//     Woman feed cat

//     Woman feed BlackCat

//     Woman feed WhiteCat

 

     }

}

 

你可能感兴趣的:(Java语言静态多分派 与 动态单分派)