Java:簡單的多態實例


多態:

多態指同一個實體同時具有多種形式。它是面向對象程序設計(OOP)的一個重要特征。如果一個語言只支持類而不支持多態,只能說明它是基於對象的,而不是面向對象的。
* 多態是出現在具有繼承關系的兩個類的對象之間,所以它不像方法重載(發生在一個類中)在編譯期間發生(也就是確定下來),而是在運行期間發生(確定下來)。*

  • 一個父類類型的引用可以指向他任何一個子類的對象
  • [相同]類域的[不同]對象執行[同一]方法的時候會有[不同]的表現

有一個比較經典的多態實例:
有一個Animal類,它有Cat,和Dog兩個子類,在Animal中有個say方法,當Cat調用這個方法的時候輸出的是“小貓喵喵喵”,當Dog調用這個方法時,輸出的是“小狗汪汪汪”,這就是Java多態的實現。

/*
對象的多態性:動物 x = new 貓();
函數的多態性:函數重載、重寫
  
1、多態的體現
        父類的引用指向了自己的子類對象
        父類的引用也可以接收自己的對象
2、多態的前提
        必須是類與類之間只有關系,要么繼承或實現
        通常還有一個前提,存在覆蓋
3、多態的好處
        多態的出現大大的提高了程序的擴展性
4、多態的弊端
        只能使用父類的引用訪問父類的成員
5、多態的應用
  
6、注意事項
*/ 
   
/*
需求:
貓,狗。
*/ 
   
abstract class Animal 
{ 
    abstract void eat(); 
} 
   
class Cat extends Animal 
{ 
    public void eat() 
    { 
        System.out.println("吃魚"); 
    } 
    public void catchMouse() 
    { 
        System.out.println("抓老鼠"); 
    } 
} 
   
class Dog extends Animal 
{ 
    public void eat() 
    { 
        System.out.println("吃骨頭"); 
    } 
    public void kanJia() 
    { 
        System.out.println("看家"); 
    } 
} 
   
class DuoTaiDemo 
{ 
    public static void main(String[] args) 
    { 
        function(new Cat()); 
        function(new Dog()); 
           
        Animal a = new Cat();//向上轉型 
        a.eat(); 
           
        Cat c = (Cat)a;//向下轉型 
        c.catchMouse(); 
           
           
    } 
       
    public static void function(Animal a) 
    { 
        a.eat(); 
        //用於子類型有限 
        //或判斷所屬類型進而使用其特有方法 
        if(a instanceof Cat) 
        { 
            Cat c = (Cat)a; 
            c.catchMouse(); 
        } 
        else if(a instanceof Dog) 
        { 
            Dog c = (Dog)a; 
            c.kanJia(); 
        } 
    }    
}   

  

 

 

這里我們寫另外一個:
—父類:Person.java

import java.util.Scanner;

public class Person {
    public int salary;
    public int allSalary(){
        return 0;
    }
    public static void main(String[] args) {
        Person p = null;

        for (int n = 0; n < 3; n++) {
            @SuppressWarnings("resource")
            Scanner sc = new Scanner(System.in);
            System.out.println("請輸入一個1-3的數字:\n1 is Teacher\n2 is Scientis\n3 is Waiter");
            int i = sc.nextInt();
            //分別指向了不同的子類,輸出的結果是不一樣的
            if (i == 1) {
                p = new Teacher(5000);
                System.out.println(p.allSalary());
            } else if (i == 2) {
                p = new Scientist(5000);
                System.out.println(p.allSalary());
            } else if (i == 3) {
                p = new Waiter(5000);
                System.out.println(p.allSalary());
            } else {
                System.out.println("?\n請輸入1-3");
            }

        }
}
}

—子類:Scientist .java

public class Scientist extends Person{
    public Scientist(int salary){
        this.salary = salary;
    }

    public int allSalary(){
        int aa = salary*12+36500;
        System.out.print("五五開一年的工資:");
        return aa;
    }
}

Waiter.java

public class Waiter extends Person{
    public Waiter(int salary){
        this.salary = salary;
    }

    public int allSalary(){
        int aa = salary*12;
        System.out.print("服務員一年的工資:");
        return aa;
    }
}

Teacher .java

public class Teacher extends Person{
    public Teacher(int salary){
        this.salary = salary;
    }

    public int allSalary(){
        int aa = salary*12+3650;
        System.out.print("教師一年的工資:");
        return aa;
    }
}

  

  

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM