Java(多態)動手動腦

來源:互聯網
上載者:User

標籤:++   隱藏   idt   print   退出   class   引用   而不是   並且   

1>

請看以下“變態”的類(參看樣本ParentChildTest.java)

上述代碼的特點是: 子類和父類定義了一模一樣的欄位和方法

運行以下測試代碼

1. 上邊的程式運行結果是什嗎?

2. 你如何解釋會得到這樣的輸出?

第一個100:是parent調用PrintValue方法輸出parent類中myValue值為100;

第二個200:是child調用PrintValue方法輸出child類中myValue值為200;

第三個200:是parent = child,將子類對象的賦值給父類,但此時parent只能調用子類的方法,調用子類PrintValue時,輸出的當然是子類的myValue值200。

第四個200:雖然parent = child,但parent不能調用子類的變數,所以parent.myValue++;只是將父類對象中的變數myValue值+1但不能改變子類的myValue值,但調用的是子類PrintValue方法時,輸出的是子類的myValue值200。

第五個201:(Child)parent,強制轉化parent為child對象,此時parent完全成為child對象,所以輸出值為201。

3. 電腦是不會出錯的,之所以得到這樣的運行結果也是有原因的, 那麼從這些運行結果中,你能總 結出Java的哪些文法特性?

(1):當子類和父類擁有同名方法時,並且讓一個父類變數引用一個子類對象時,調用哪個方法由自己的真實類型來決定。

(2):如果子類與父類有相同的欄位,則子類中的欄位會代替或隱藏父類的欄位,子類方法中訪問的是子類中的欄位(而不是父類中的欄位)。

2>

用多態的方法類比ATM操作流程。

import java.util.Scanner;abstract class atma{    public abstract void QuKuan();    public abstract void CunKuan();    public abstract void ZhuanZhang();    public abstract void updataMima(String str);    public abstract void showMoney();}public class ATM {    public static void main(String[] args) {        // TODO Auto-generated method stub        Account A = new Account();        A.display();    }}class Account extends atma{    String account;    String name;    String data;    int leixing;    String mima;    double money;    Account(){}    Account(String account,String name,String data,String mima,double money)    {        this.account = account;        this.name = name;        this.data = data;        this.mima = mima;        this.money = money;    }    public String getMima()    {        return mima;    }        public void Mune()    {        System.out.println("1.取款");        System.out.println("2.存款");        System.out.println("3.轉賬匯款");        System.out.println("4.修改密碼");        System.out.println("5.查詢餘額");        System.out.println("6.退卡");    }    public void QuKuan()    {        System.out.println("100元");        System.out.println("500元");        System.out.println("1000元");        System.out.println("1500元");        System.out.println("2000元");        System.out.println("5000元");        System.out.println("其他金額");        System.out.println("返回");        Scanner in = new Scanner(System.in);        System.out.println("請輸入取款金額:");        int M = in.nextInt();        if(M <= money)        {            System.out.println("正在取款請稍後。。。");            money = money - M;            System.out.println("取款成功!");        }        else        {            System.out.println("餘額不足,取款失敗!");        }    }    public void CunKuan()    {        Scanner in = new Scanner(System.in);        System.out.println("請輸入存款金額:");        int M = in.nextInt();        money = money + M;        System.out.println("存款成功!");    }    public void ZhuanZhang()    {        Scanner in = new Scanner(System.in);        System.out.println("請輸入轉賬行號:");        String H = in.next();        System.out.println("你要轉賬的人的姓名是否為xxx?0:是,1:否");        int X=in.nextInt();        if(X==0)        {            System.out.println("請輸入轉賬金額:");            int M = in.nextInt();            if(money >= M)            {                System.out.println("轉賬成功!");                money = money - M;            }            else            {                System.out.println("餘額不足,轉賬失敗!");            }        }        if(X==1)        {            System.out.println("卡號錯誤!");        }    }    public void updataMima(String str)    {        if(str.equals(mima))        {            System.out.println("新密碼不得與舊密碼重複!");        }        else        {            mima = str;            System.out.println("修改成功!");        }    }    public void showMoney()    {        System.out.println("您的賬戶餘額為:"+money);    }    public void display()    {        Scanner in = new Scanner(System.in);        Account[] a = new Account[100];        a[0] = new Account("123456789101","ctt","2016/11/15","123456",1000);        System.out.println( "請輸入密碼:" );        String input = in.next();        boolean flag = false;        for(int i  = 0;i<50;i++)        {            if(a[i].getMima().equals(input)&& a[i]!=null)                flag = true;            break;        }        int s1;        if(flag)        {            boolean p = true;            while(p)            {            Mune();            System.out.println( "請輸入要執行的操作:" );            s1 = in.nextInt();            if(s1 == 1)            {                QuKuan();                continue;            }            if(s1 == 2)            {                CunKuan();                continue;            }            if(s1 == 3)            {                ZhuanZhang();                continue;            }            if(s1 == 4)            {                System.out.println("請輸入新密碼:");                String str = in.next();                updataMima(str);                continue;            }            if(s1 == 5)            {                showMoney();                continue;            }            else            {                p = false;                System.out.println("已退出!");            }        }        }                else        {            System.out.println("輸入錯誤!");        }    }}

結果:

分析:利用抽象類別實現。

 

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.