實(shí)驗(yàn)二-面向?qū)ο蟪绦蛟O(shè)計(jì)_第1頁(yè)
實(shí)驗(yàn)二-面向?qū)ο蟪绦蛟O(shè)計(jì)_第2頁(yè)
實(shí)驗(yàn)二-面向?qū)ο蟪绦蛟O(shè)計(jì)_第3頁(yè)
實(shí)驗(yàn)二-面向?qū)ο蟪绦蛟O(shè)計(jì)_第4頁(yè)
實(shí)驗(yàn)二-面向?qū)ο蟪绦蛟O(shè)計(jì)_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上EXP2課題(項(xiàng)目)名稱: 實(shí)驗(yàn)二 面向?qū)ο蟪绦蛟O(shè)計(jì)計(jì)劃學(xué)時(shí):2 實(shí)驗(yàn)類型: 1.演示性 2.驗(yàn)證性 3.綜合性 4.設(shè)計(jì)性 5.其它授課日期: 年 月 日 第 周 星期 第 節(jié)實(shí)驗(yàn)?zāi)康?. 驗(yàn)證面向?qū)ο笕筇匦?. 學(xué)習(xí)封裝的實(shí)現(xiàn)3. 學(xué)習(xí)繼承的實(shí)現(xiàn)4. 編寫多態(tài)實(shí)例5. 學(xué)習(xí)抽象類的使用6. 學(xué)習(xí)接口的使用實(shí)驗(yàn)要求1. 掌握封裝的實(shí)現(xiàn)方法2. 掌握繼承的編程方式和思想3. 理解多態(tài)現(xiàn)象4. 掌握抽象類和接口的使用實(shí)驗(yàn)內(nèi)容與步驟1. 封裝的實(shí)現(xiàn)(1) 編寫程序模擬個(gè)人銀行賬號(hào)類。考慮個(gè)人銀行的特點(diǎn),建立類模型(注意屬性和方法的訪問權(quán)限修飾符)參考代碼public c

2、lass BankAccount private String accountID;private String password;private int balance;public BankAccount(String accountID,String password,String operator)this.accountID=accountID;this.password=password;this.balance=0;System.out.println("Create a BankAccount");System.out.println("Accou

3、ntID:"+this.accountID);System.out.println("Current Balance:"+this.balance);System.out.println("Operator:"+operator);System.out.println("Save Account info to Database");public void queryBalance(String password)if(password=this.password)System.out.println("Passw

4、ord OK");System.out.println("Current Account Balance:"+this.balance);elseSystem.out.println("Password Erro");public void changePassword(String oldPassword,String newPassword)if(oldPassword=this.password)System.out.println("Password OK");this.password=newPassword;Sy

5、stem.out.println("Change Passord OK");elseSystem.out.println("Password Erro");public void deposit(int money,String operator)this.balance+=money;System.out.println("add balance OK.Operator:"+operator);System.out.println("Save Account change to database");public

6、 void withdraw(String password,int money,String operator)if(password=this.password)System.out.println("Password OK");if(this.balance>money)this.balance-=money;System.out.println("withdraw:"+money+" ok, Operator:"+operator );System.out.println("Current Account Ba

7、lance:"+this.balance);System.out.println("Save Account change to database");elseSystem.out.println("withdraw:"+money+" erro. Because of not enough balance");elseSystem.out.println("Password Erro");(2) 編寫測(cè)試類,完成如下(1)中類方法的測(cè)試實(shí)現(xiàn)如下業(yè)務(wù):開戶,存款100,查詢余額,取款50,查詢余額,取款2

8、00,查詢余額public class Test1 public static void main(String args) BankAccount bankaccount =new BankAccount("", "", "TOM");bankaccount.queryBalance("");bankaccount.changePassword("","");bankaccount.deposit(100,"TOM");bankaccount.withd

9、raw("",50,"TOM");2. 繼承的實(shí)現(xiàn)(1) 按如下類圖編寫代碼 參考代碼class Person String id;String name;String age;public void sleep() System.out.println("I am Person,I am sleeping");public void eat() System.out.println("I am Person,I am eating");class Student extends Person String sno

10、;public void study() System.out.println("I am Student,I am studying");class Teacher extends Person String tid;public void tech() System.out.println("I am Student,I am taching");(2) 編寫測(cè)試類并創(chuàng)建main()方法,完成如下操作A 分別創(chuàng)建Person、Student、Teacher對(duì)象,完成屬性和每個(gè)方法的調(diào)用測(cè)試B 在Student和Teacher中完成eat()方法的重寫

11、C 編寫類型轉(zhuǎn)化示例(向上類型轉(zhuǎn)化、向下類型轉(zhuǎn)化)3. 多態(tài)現(xiàn)象 在完成(2)中Student、Teacher類eat()方法重寫后,在測(cè)試類中編寫如下代碼并在main()方法中調(diào)用static void askAllToEat(Person ps) for(int i=0;i<ps.length;i+) psi.eat(); psi.sleep(); 從以上代碼中體會(huì)多態(tài)現(xiàn)象。public class Test2 public static void main(String args) Person person = new Person();person.sleep();person

12、.eat();Student student = new Student();student.sleep();student.eat();student.study();Teacher teacher = new Teacher();teacher.sleep();teacher.eat();teacher.tech();static void askAllToEat(Person ps) for (int i = 0; i < ps.length; i+) psi.eat();psi.sleep();4. 抽象類的使用編寫以上類圖所示類,并編寫測(cè)試代碼測(cè)試抽象類的使用參考代碼abstr

13、act class Printerprivate String printerType;Printer(String printerType)this.printerType=printerType;abstract void print(String txt);void showMyType()System.out.println("My Type is:"+printerType);class InkPrinter extends PrinterInkPrinter(String inkPrinterType)super(inkPrinterType);void pri

14、nt(String txt)System.out.println("I am Ink Printer");System.out.println("Start to Print:"+txt);class LasertPrinter extends PrinterLasertPrinter(String laserPrinterType)super(laserPrinterType);void print(String txt)System.out.println("I am Lasert Printer");System.out.pri

15、ntln("Start to Print:"+txt);編寫測(cè)試類及main() 方法,完成如下操作A. 創(chuàng)建Printer,InkPrinter,LaserPrinter類的對(duì)象。B. 設(shè)計(jì)并編寫演示多態(tài)現(xiàn)象的代碼5. 接口的使用編寫以上類圖的代碼,并編寫測(cè)試類測(cè)試接口的使用參考代碼interface IScanvoid scan();abstract class Printerprivate String printerType;Printer(String printerType)this.printerType=printerType;abstract void p

16、rint(String txt);void showMyType()System.out.println("My Type is:"+printerType);abstract class InkPrinter extends PrinterInkPrinter(String inkPrinterType)super(inkPrinterType);void print(String txt)System.out.println("I am Ink Printer");System.out.println("Start to Print:"+txt);class LasertPrinter extends PrinterLasert

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論