<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java——多態(tài)

          共 3110字,需瀏覽 7分鐘

           ·

          2021-06-27 22:13

          目錄

                  概述
                  成員訪問特點
                  優(yōu)缺點
                  多態(tài)中的轉(zhuǎn)型

          概述

          同一個對象,在不同時刻表現(xiàn)出來的不同形態(tài)。
          例:貓
          貓是貓:
          貓 cat = new 貓();
          貓是動物:動物 animal = new 貓();
          此處,貓在不同時刻表現(xiàn)出來了不同形態(tài),這就是多態(tài)。
          多態(tài)的前提和體現(xiàn):
          ①有繼承/實現(xiàn)關(guān)系
          ②有方法重寫
          ③有父類引用指向子類
          /*
          動物類(父類)
          */

          public class Animal {
          public void eat(){
          System.out.println("動物吃什么");
          }
          }
          /*
          貓類(子類)
          */

          public class Cat extends Animal {
          @Override
          public void eat() {
          System.out.println("貓吃魚");
          }
          }
          /*
          測試類
          */

          public class AnimalDemo {
          public static void main(String[] args){
          //父類引用指向子類對象
          Animal a = new Cat();
          }
          }

          成員訪問特點

          成員變量:編譯看左邊,執(zhí)行看左邊
          成員方法:編譯看左邊,執(zhí)行看右邊
          /*
          動物類(父類)
          */

          public class Animal {
          public int age = 20;

          public void eat() {
          System.out.println("動物吃什么");
          }
          }
          /*
          貓類(子類)
          */

          public class Cat extends Animal {
          public int age = 2;
          public int weight = 10;

          @Override
          public void eat() {
          System.out.println("貓吃魚");
          }

          public void playGame() {
          System.out.println("貓捉老鼠");
          }
          }
          /*
          測試類
          */

          public class AnimalDemo {
          public static void main(String[] args) {
          //父類引用指向子類對象
          Animal a = new Cat();

          System.out.println(a.age);
          //System.out.println(a.weight);
          System.out.println("------");
          a.eat();
          //a.playGame();
          }
          }


          優(yōu)缺點

          優(yōu)點:提高了程序的擴展性
          定義方法的時候,使用父類型作為參數(shù),將來使用時,使用具體的子類型參與操作。
          缺點:不能使用子類的特有功能。
          /*
          動物類(父類)
          */

          public class Animal {

          public void eat() {
          System.out.println("動物吃什么");
          }
          }
          /*
          貓類(子類)
          */

          public class Cat extends Animal {

          @Override
          public void eat() {
          System.out.println("貓吃魚");
          }
          }
          /*
          小狗類(子類)
          */

          public class Dog extends Animal {
          @Override
          public void eat() {
          System.out.println("狗吃骨頭");
          }

          public void lookDog() {
          System.out.println("狗看門");
          }
          }
          /*
          動物操作類
          */

          public class AnimalOperator {

          public void useAnimal(Animal a) {
          a.eat();
          // a.lookDog();//不能使用子類的特有功能
          }
          /*
          public void useAnimal(Cat c) {
          c.eat();
          }
          public void useAnimal(Dog d) {
          d.eat();
          }
          */

          }
          /*
          測試類
          */

          public class AnimalDemo {
          public static void main(String[] args) {
          AnimalOperator ao = new AnimalOperator();
          Cat c = new Cat();
          ao.useAnimal(c);

          Dog d = new Dog();
          ao.useAnimal(d);
          }
          }


          多態(tài)中的轉(zhuǎn)型

          • 向上轉(zhuǎn)型
            從子類到父類,父類引用指向子類對象


          • 向下轉(zhuǎn)型
            從父類到子類,父引用轉(zhuǎn)為子類對象


          /*
          動物類(父類)
          */

          public class Animal {

          public void eat() {
          System.out.println("動物吃什么");
          }
          }
          /*
          貓類(子類)
          */

          public class Cat extends Animal {

          @Override
          public void eat() {
          System.out.println("貓吃魚");
          }

          public void playGame() {
          System.out.println("貓捉老鼠");
          }
          }
          /*
          測試類
          */

          public class AnimalDemo {
          public static void main(String[] args) {
          //父類引用指向子類對象
          //向上轉(zhuǎn)型
          Animal a = new Cat();
          a.eat();
          System.out.println("------");

          //向下轉(zhuǎn)型
          Cat c = (Cat) a;
          c.eat();
          c.playGame();
          }
          }






          往期推薦
          01

          實現(xiàn)一個小型的學生信息管理系統(tǒng)

          02

          ArrayList集合

          03

          Java——修飾符





          在看點一下
          瀏覽 41
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品久草 | 久久精品熟女亚洲AV蜜桃 | www.亚洲电影 | 果冻传媒91cm-084换妻下部董小宛 | 麻豆A∨在线 |