java-08多態與異常處理

來源:互聯網
上載者:User

標籤:http   string   改變   port   images   決定   ati   中文   parent   

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);    }}

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

第一個是父類自身的對象,同時調用了父類自己的方法。第二個是子類自身的對象調用了子類自己的方法。

第三個父類變數引用子類對象調用子類的方法。第四個引用了子類的對象,調用了子類的方法。

第五個通過類型轉換改變了子類的變數,用了子類的方法。

當子類與父類擁有一樣的方法,並且讓一個父類變數引用一個子類對象時,到底調用哪個方法,由對象自己的“真實”類型所決定,這就是說:對象是子類型的,它就調用子類型的方法,是父類型的,它就調用父類型的方法。

2.

import java.util.Scanner;class Account{    String account;    String name;    String date;    int key;    double money;        public Account(String a,String b,String c,int d,double e)    {        account=a;        name=b;        date=c;        key=d;        money=e;    }    }public class Option {    public static void main(String[] args) {        Account aa=new Account("622888040311","zxw","2016.11.15",123456,1000);        int a,b;        System.out.println("請選擇語言:1.中文 2.英文");        Scanner in =new Scanner(System.in);        a=in.nextInt();        System.out.println("請輸入密碼:");        Scanner in1 =new Scanner(System.in);        b=in1.nextInt();        if(b==aa.key){            while(true){                            int choose;            System.out.println("請選擇操作:1.存款 2.取款 3.轉賬匯款 4.修改密碼 5.查詢餘額");            Scanner in2 =new Scanner(System.in);            choose=in2.nextInt();            if(choose==1){                System.out.println("請輸入存款金額:");                Scanner in3 =new Scanner(System.in);                double cun=in3.nextInt();                aa.money=aa.money+cun;                continue;            }            else if(choose==2){                System.out.println("請輸入取款金額:");                Scanner in4=new Scanner(System.in);                double qu=in4.nextDouble();                if(aa.money<qu){                    System.out.println("卡內餘額不足");                }                aa.money=aa.money-qu;                continue;            }            else if(choose==3){                System.out.println("請輸入要轉賬的卡號:");                Scanner in6=new Scanner(System.in);                String hang=in6.nextLine();                if(hang.equals(aa.account)){                    System.out.println("請輸入轉賬金額:");                    Scanner in5=new Scanner(System.in);                    double zhuan=in5.nextDouble();                    aa.money=aa.money-zhuan;                }            }            else if(choose==4){                System.out.println("請輸入原密碼:");                Scanner in7=new Scanner(System.in);                int mi=in7.nextInt();                if(mi==aa.key){                    System.out.println("請輸入新密碼:");                    Scanner in8=new Scanner(System.in);                    int mi2=in8.nextInt();                    aa.key=mi2;                }            }            else if(choose==5){                System.out.println("現在的餘額為:"+aa.money);            }                        }        }        else            System.out.println("密碼錯誤,該卡已被鎖定,無法操作");    }}

 

java-08多態與異常處理

聯繫我們

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