ppt 8
1.運(yùn)行以下測(cè)試代碼:
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();//調(diào)用子類的方法,方法中是子類的變量 ????????? ????????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); ????} }?
?
?
分析總結(jié):
1.new的parent類的對(duì)象,所以在調(diào)用時(shí)調(diào)用的是parent的構(gòu)造方法以及parent的value值
2.new的child的類的對(duì)象,所以在調(diào)用構(gòu)造方法時(shí)調(diào)用child的構(gòu)造方法以及child的value值
3.將子類的child賦值給了他的父類parent,在調(diào)用構(gòu)造方法時(shí)也就是調(diào)用子類child的構(gòu)造方法,但是在子類的構(gòu)造方法時(shí)的value是子類child的value值,所以是子類child的構(gòu)造方法加上value=200
4.parent的value值加一,但是parent仍是前面的child賦過(guò)值的,所以調(diào)用的仍然是child的構(gòu)造方法,value也是child中的value所以仍是200
5.將parent賦給child類,所以parent也是child的一部分,所以調(diào)用時(shí)是調(diào)用子類的構(gòu)造方法,并且之前value值加一了嗎,所以輸出201
6.如果子類與父類有相同的字段,則子類中的字段會(huì)代替或隱藏父類的字段,子類方法中訪問(wèn)的是子類中的字段(而不是父類中的字段)。如果子類方法確實(shí)想訪問(wèn)父類中被隱藏的同名字段,可以用super關(guān)鍵字來(lái)訪問(wèn)它。
?
2.用多態(tài)的方法模擬ATM操作流程。
import?java.util.Scanner;
?public?class?showAtm { ?????@SuppressWarnings("resource") ?????public?static?void?main(String[] args) { ???????ATM atm=new?ATM(); ???????showAtm s=new?showAtm(); ???????s.showFace(); ???????atm.select(); ?????} ?????//顯示菜單方法 ???public?static?void?showFace(){ ?????????System.out.println("********************"); ?????????System.out.println("??? 1.存款:"); ?????????System.out.println("??? 2.取款:"); ?????????System.out.println("??? 3.轉(zhuǎn)賬匯款:"); ?????????System.out.println("??? 4.修改密碼:"); ?????????System.out.println("??? 5.查詢余額:"); ?????????System.out.println("********************"); ?????????System.out.println("請(qǐng)選擇:"); ?????} ?} class?PersonalAccount{ ?????String account; ????????String name; ????????String date; ????????String mima; ????????double?yue; ????????PersonalAccount(String account,String name,String date,String mima,double?yue){ ????????????this.account=account; ????????????this.name=name; ????????????this.date=date; ????????????this.mima=mima; ????????????this.yue=yue; ????????} ????????String Getaccount() ????????{ ????????????return?account; ????????} ????????String Getname() ????????{ ????????????return?name; ????????} ????????String Getdata() ????????{ ????????????return?date; ????????} ????????String Getmima() ????????{ ????????????return?mima; ????????} ????????double?Getyue() ????????{ ????????????return?yue; ????????} ????????void?Setaccount(String account) ????????{ ????????????this.account=account; ????????} ????????void?Setname(String name) ????????{ ????????????this.name=name; ????????} ????????void?Setdate(String date) ????????{ ????????????this.date=date; ????????} ????????void?Setmima(String mima) ????????{ ????????????this.mima=mima; ????????} ????????void?Setyue(double?yue) ????????{ ????????????this.yue=yue; ????????} ?} ?abstract?class?aATM{ ?????public?abstract?void?QuKuan();//取款 ?????public?abstract?void?CunKuan();//存款 ?????public?abstract?void?Zhuanzhang();//轉(zhuǎn)賬 ?????public?abstract?void?mima();//改密碼 ?????public?abstract?void?yue();//余額 ?} ?class?ATM?extends?aATM{ ?????Scanner in =?new?Scanner(System.in); ????PersonalAccount A=new?PersonalAccount("1234567890123","han","20161115","970318",1000); ????public?void?QuKuan(){ ????????System.out.println("可取款金額:"); ????????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("請(qǐng)輸入你要取款的金額:"); ????????double?Q=in.nextDouble(); ????????if(A.yue-Q<0) ????????{ ????????????System.out.println("余額不足!"); ????????} ????????else ????????{ ????????System.out.println("取款成功!"); ?????????A.yue=A.yue-Q; ????????? ????????} ????} ????public?void?CunKuan(){ ????????System.out.println("請(qǐng)輸入存款金額:"); ????????double?Q1=in.nextDouble(); ????????A.yue=A.yue+Q1; ??????} ????public?void?Zhuanzhang(){ ????????System.out.println("輸入要轉(zhuǎn)賬的行號(hào):"); ????????String H=in.next(); ????????System.out.println("你要轉(zhuǎn)賬的人的姓名是否為xxx?0:是,1:否"); ????????int?X=in.nextInt(); ????????if(X==0) ????????{ ????????????System.out.println("請(qǐng)輸入要轉(zhuǎn)賬的金額:"); ????????????double?Z=in.nextDouble(); ????????????System.out.println("轉(zhuǎn)賬成功!"); ????????????A.yue=A.yue-Z; ????????????System.out.println("您的余額為:"+A.yue); ????????} ????????if(X==1) ????????{ ????????????System.out.println("卡號(hào)錯(cuò)誤!"); ????????} ????} ????public?void?mima(){ ????????System.out.println("請(qǐng)輸入您要修改的卡號(hào):"); ????????String S=in.next(); ????????if(A.Getaccount().equals(S)) ????????{ ????????????System.out.println("請(qǐng)輸入原密碼:"); ????????????String S1=in.next(); ????????????if(A.Getmima().equals(S1)) ????????????{ ????????????????System.out.println("請(qǐng)輸入新密碼:"); ????????????????String S2=in.next(); ????????????????A.Setmima(S2); ????????????????System.out.println("修改成功!"); ????????????} ????????} ????} ????public?void?yue() ????{ ????????System.out.println("余額為:"+A.yue); ????} ?void?select(){ ????boolean?p=true; ????while(p ==?true) ????{ ????Scanner in =?new?Scanner(System.in); ????System.out.println("請(qǐng)輸入要執(zhí)行的操作:"); ????int?m = in.nextInt(); ????if(m ==?1) ????{ ????????QuKuan(); ????????continue; ????} ????if(m ==?2) ????{ ????????CunKuan(); ????????continue; ????????? ????} ????if(m ==?3) ????{ ????????Zhuanzhang(); ????????continue; ????} ????if(m ==?4) ????{ ????????mima(); ????????continue; ????} ????? ????if(m ==?5) ??????{ ????????yue(); ????????continue; ??????} ????else ??????{ ????????System.out.println("已退出系統(tǒng)!"); ????????p =?false; ??????} ??????in.close(); ?????} ??} ?}?
?
3.
下列語(yǔ)句哪一個(gè)將引起編譯錯(cuò)誤?為什么?哪一個(gè)會(huì)引起運(yùn)行時(shí)錯(cuò)誤?為什么?
m=d;
d=m;
d=(Dog)m;
d=c;
c=(Cat)m;
運(yùn)行以下代碼:
class?Mammal{}
class?Dog?extends?Mammal {} class?Cat?extends?Mammal{} public?class?TestCast { ????public?static?void?main(String args[]) ????{ ????????Mammal m; ????????Dog d=new?Dog(); ????????Cat c=new?Cat(); ????????m=d; ????????d=m; ????????d=(Dog)m; ????????d=c; ????????c=(Cat)m; ????} }?
d是dog的對(duì)象,m是Mammal的對(duì)象,所以這相當(dāng)于直接將父類對(duì)象給子類要用強(qiáng)制類型轉(zhuǎn)換
c,d是同一級(jí),同為子類,不能相互賦值。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/muxiaozhou/p/6079458.html
總結(jié)
- 上一篇: HTML5 - Canvas动画样例(谷
- 下一篇: 互联网汽车迎新成员 Alibaba Yu