




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優質文檔-傾情為你奉上EXP2課題(項目)名稱: 實驗二 面向對象程序設計計劃學時:2 實驗類型: 1.演示性 2.驗證性 3.綜合性 4.設計性 5.其它授課日期: 年 月 日 第 周 星期 第 節實驗目的1. 驗證面向對象三大特性2. 學習封裝的實現3. 學習繼承的實現4. 編寫多態實例5. 學習抽象類的使用6. 學習接口的使用實驗要求1. 掌握封裝的實現方法2. 掌握繼承的編程方式和思想3. 理解多態現象4. 掌握抽象類和接口的使用實驗內容與步驟1. 封裝的實現(1) 編寫程序模擬個人銀行賬號類??紤]個人銀行的特點,建立類模型(注意屬性和方法的訪問權限修飾符)參考代碼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) 編寫測試類,完成如下(1)中類方法的測試實現如下業務:開戶,存款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. 繼承的實現(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) 編寫測試類并創建main()方法,完成如下操作A 分別創建Person、Student、Teacher對象,完成屬性和每個方法的調用測試B 在Student和Teacher中完成eat()方法的重寫
11、C 編寫類型轉化示例(向上類型轉化、向下類型轉化)3. 多態現象 在完成(2)中Student、Teacher類eat()方法重寫后,在測試類中編寫如下代碼并在main()方法中調用static void askAllToEat(Person ps) for(int i=0;i<ps.length;i+) psi.eat(); psi.sleep(); 從以上代碼中體會多態現象。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. 抽象類的使用編寫以上類圖所示類,并編寫測試代碼測試抽象類的使用參考代碼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);編寫測試類及main() 方法,完成如下操作A. 創建Printer,InkPrinter,LaserPrinter類的對象。B. 設計并編寫演示多態現象的代碼5. 接口的使用編寫以上類圖的代碼,并編寫測試類測試接口的使用參考代碼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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025《設備采購合同模板》
- 2025技術開發委托合同
- 2025計算機軟件購銷合同范本
- 2025財產信托合同樣本
- 2025年太陽能組件生產裝備項目建議書
- 2025年專用改性型氯化聚乙烯合作協議書
- 2025年稀有金屬及稀土金屬材料合作協議書
- 2025年聚氧乙烯醚項目建議書
- 2025年高純高碳鉻軸承鋼及滲碳軸承鋼合作協議書
- 樓房打樁施工方案
- CAD輸入文字時提示“找不到主詞典無法啟動拼寫檢查程序”怎么辦
- -活出心花怒放的生命 課件 心理健康
- 給水泵檢修方案
- 設備出入庫管理辦法
- KEGG代謝通路中文翻譯
- 消火栓月檢查表
- GB∕T 17832-2021 銀合金首飾 銀含量的測定 溴化鉀容量法(電位滴定法)
- 低成本自動化的開展與案例77頁PPT課件
- 人防工程竣工資料(全套)
- 梅州市部分飲用水源保護區調整方案
- “重慶環?!睒酥菊f明
評論
0/150
提交評論