Java語言靜態多指派 與 動態單指派

來源:互聯網
上載者:User

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

 

     }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.