JAVA多態問題總結(課堂總結)

來源:互聯網
上載者:User

標籤:union   put   next   dsl   vfp   zab   arp   ssg   發送訊息   

物件導向的三大特性:封裝、繼承、多態。從一定角度來看,封裝和繼承幾乎都是為多態而準備的。這是我們最後一個概念,也是最重要的知識點。
多態的定義:指允許不同類的對象對同一訊息做出響應。即同一訊息可以根據發送對象的不同而採用多種不同的行為方式。(發送訊息就是函數調用)
(1)相同方法名的方法分別在子,基類中調用
原始碼:
public class ParentChildTest {public static void main(String[] args) {Parent parent=new Parent();parent.printValue();Child child=new Child();child.printValue();parent=child;parent.printValue();parent.myValue++;parent.printValue();((Child)parent).myValue++;parent.printValue();}}class Parent{public int myValue=100;public void printValue() {System.out.println("Parent.printValue(),myValue="+myValue);}}class Child extends Parent{public int myValue=200;public void printValue() {System.out.println("Child.printValue(),myValue="+myValue);}}

  實驗結果:

 分析:當子類與父類擁有一樣的方法,並且讓一個父類變數引用一個子類對象時,到底調用哪個方法,由對象自己的“真實”類型所決定,這就是說:對象是子類型的,它就調用子類型的方法,是父類型的,它就調用父類型的方法。
如果子類與父類有相同的欄位,則子類中的欄位會代替或隱藏父類的欄位,子類方法中訪問的是子類中的欄位(而不是父類中的欄位)。如果子類方法確實想訪問父類中被隱藏的同名欄位,可以用super關鍵字來訪問它。
(2)用多態方法類比ATM操作流程
原始碼:
package bokeyuan;import java.util.Scanner;public class ATMTest {    public static void main(String[] args) {        ABCATM atm=new ABCATM();        UnionPay abc=new ABCImpl(3000,1603);        atm.insertCard(abc);        atm.allMenu();          }}//農行ATMclass ABCATM{    UnionPay card;    Scanner sc=new Scanner(System.in);    public void insertCard(UnionPay card){        this.card=card;    }    public void outCard(){        card=null;    }    public  void showBalance(){   //顯示餘額        System.out.println("卡內餘額:"+card.getBalance());    }    public void takeMoney(){    //取錢        System.out.println("請輸入取錢金額:");        double number=sc.nextDouble();        if(card.drawMoney(number)){            System.out.println("取款成功!");        }else{            System.out.println("取款失敗!");        }    }     public void payTelBill(){   //電話繳費        if(card instanceof ABCImpl){            ABCImpl abccard=(ABCImpl)card;            System.out.println("請輸入電話號碼:");            String phoneNum=sc.next();            while(phoneNum.length()!=11){                System.out.println("請輸入電話號碼:");                phoneNum=sc.next();            }            if(phoneNum.length()==11){                System.out.println("請輸入繳費金額:");                double calls=sc.nextDouble();                if(abccard.payTel(phoneNum,calls)){                    System.out.println("繳費成功!");                                    }else{                    System.out.println("繳費失敗!");                }            }else{                System.out.println("您輸入的電話號碼有誤!");            }        }else{            System.out.println("您的銀行卡非農行卡,不支援電話繳費");        }    }    public void allMenu(){   //功能菜單        do{            System.out.println("請輸入密碼:");            int input=sc.nextInt();            if(card.checkPwd(input)){                System.out.println("請選擇功能:1.查詢餘額 2.電話繳費 3.取款 0.退出");                int a=sc.nextInt();                if(a==0){                    System.out.println("退出系統!");                    break;                }else{                    switch(a){                    case 1:showBalance();                           break;                    case 2:payTelBill();                           break;                    case 3:takeMoney();                           break;                    default:System.out.println("非法輸入!");                    }                }            break;            }else{                System.out.println("密碼錯誤!");                System.out.println("請輸入密碼:");                input=sc.nextInt();            }        }while(true);    }}//銀聯結口interface UnionPay {        public boolean drawMoney(double number); //取錢        public boolean checkPwd(int input);  //驗證密碼        public double getBalance();      //顯示餘額}//農行卡介面interface ABC extends UnionPay{    public boolean payTel(String phoneNum,double calls); //支付電話費}//實現農行卡介面class ABCImpl implements ABC{    private double money;    private int pwd;    public ABCImpl(double money,int pwd){        this.money=money;        this.pwd=pwd;    }    public boolean payTel(String phoneNum,double calls){        if(phoneNum.length()==11&&calls<money){            money-=calls;            return true;        }        return false;    }    public boolean drawMoney(double number){        if(number<money){            money-=number;            return true;        }        return false;    }    public boolean checkPwd(int input){        if(pwd==input){            return true;        }        return false;    }    public double getBalance(){        return money;    }   }

 

運行結果:

 

JAVA多態問題總結(課堂總結)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.