您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 实验二-面向对象程序设计
EXP2课题(项目)名称:实验二面向对象程序设计计划学时:2实验类型:1.演示性□2.验证性□3.综合性□4.设计性□5.其它□授课日期:年月日第周星期第节实验目的1.验证面向对象三大特性2.学习封装的实现3.学习继承的实现4.编写多态实例5.学习抽象类的使用6.学习接口的使用实验要求1.掌握封装的实现方法2.掌握继承的编程方式和思想3.理解多态现象4.掌握抽象类和接口的使用实验内容与步骤1.封装的实现(1)编写程序模拟个人银行账号类。考虑个人银行的特点,建立类模型(注意属性和方法的访问权限修饰符)[参考代码]publicclassBankAccount{privateStringaccountID;privateStringpassword;privateintbalance;publicBankAccount(StringaccountID,Stringpassword,Stringoperator){this.accountID=accountID;this.password=password;this.balance=0;System.out.println(CreateaBankAccount);System.out.println(AccountID:+this.accountID);System.out.println(CurrentBalance:+this.balance);System.out.println(Operator:+operator);System.out.println(SaveAccountinfotoDatabase);}publicvoidqueryBalance(Stringpassword){if(password==this.password){System.out.println(PasswordOK);System.out.println(CurrentAccountBalance:+this.balance);}else{System.out.println(PasswordErro);}}publicvoidchangePassword(StringoldPassword,StringnewPassword){if(oldPassword==this.password){System.out.println(PasswordOK);this.password=newPassword;System.out.println(ChangePassordOK);}else{System.out.println(PasswordErro);}}publicvoiddeposit(intmoney,Stringoperator){this.balance+=money;System.out.println(addbalanceOK.Operator:+operator);System.out.println(SaveAccountchangetodatabase);}publicvoidwithdraw(Stringpassword,intmoney,Stringoperator){if(password==this.password){System.out.println(PasswordOK);if(this.balancemoney){this.balance-=money;System.out.println(withdraw:+money+ok,Operator:+operator);System.out.println(CurrentAccountBalance:+this.balance);System.out.println(SaveAccountchangetodatabase);}else{System.out.println(withdraw:+money+erro.Becauseofnotenoughbalance);}}else{System.out.println(PasswordErro);}}}(2)编写测试类,完成如下(1)中类方法的测试实现如下业务:开户,存款100,查询余额,取款50,查询余额,取款200,查询余额publicclassTest1{publicstaticvoidmain(String[]args){BankAccountbankaccount=newBankAccount(6210330710014867331,123456,TOM);bankaccount.queryBalance(123456);bankaccount.changePassword(123456,789654);bankaccount.deposit(100,TOM);bankaccount.withdraw(789654,50,TOM);}}2.继承的实现(1)按如下类图编写代码[参考代码]classPerson{Stringid;Stringname;Stringage;publicvoidsleep(){System.out.println(IamPerson,Iamsleeping);}publicvoideat(){System.out.println(IamPerson,Iameating);}}classStudentextendsPerson{Stringsno;publicvoidstudy(){System.out.println(IamStudent,Iamstudying);}}classTeacherextendsPerson{Stringtid;publicvoidtech(){System.out.println(IamStudent,Iamtaching);}}(2)编写测试类并创建main()方法,完成如下操作A.分别创建Person、Student、Teacher对象,完成属性和每个方法的调用测试B.在Student和Teacher中完成eat()方法的重写C.编写类型转化示例(向上类型转化、向下类型转化)3.多态现象在完成(2)中Student、Teacher类eat()方法重写后,在测试类中编写如下代码并在main()方法中调用staticvoidaskAllToEat(Person[]ps){for(inti=0;ips.length;i++){ps[i].eat();ps[i].sleep();}}从以上代码中体会多态现象。publicclassTest2{publicstaticvoidmain(String[]args){Personperson=newPerson();person.sleep();person.eat();Studentstudent=newStudent();student.sleep();student.eat();student.study();Teacherteacher=newTeacher();teacher.sleep();teacher.eat();teacher.tech();}staticvoidaskAllToEat(Person[]ps){for(inti=0;ips.length;i++){ps[i].eat();ps[i].sleep();}}}4.抽象类的使用编写以上类图所示类,并编写测试代码测试抽象类的使用[参考代码]abstractclassPrinter{privateStringprinterType;Printer(StringprinterType){this.printerType=printerType;}abstractvoidprint(Stringtxt);voidshowMyType(){System.out.println(MyTypeis:+printerType);}}classInkPrinterextendsPrinter{InkPrinter(StringinkPrinterType){super(inkPrinterType);}voidprint(Stringtxt){System.out.println(IamInkPrinter);System.out.println(StarttoPrint:+txt);}}classLasertPrinterextendsPrinter{LasertPrinter(StringlaserPrinterType){super(laserPrinterType);}voidprint(Stringtxt){System.out.println(IamLasertPrinter);System.out.println(StarttoPrint:+txt);}}编写测试类及main()方法,完成如下操作A.创建Printer,InkPrinter,LaserPrinter类的对象。B.设计并编写演示多态现象的代码5.接口的使用编写以上类图的代码,并编写测试类测试接口的使用[参考代码]interfaceIScan{voidscan();}abstractclassPrinter{privateStringprinterType;Printer(StringprinterType){this.printerType=printerType;}abstractvoidprint(Stringtxt);voidshowMyType(){System.out.println(MyTypeis:+printerType);}}abstractclassInkPrinterextendsPrinter{InkPrinter(StringinkPrinterType){super(inkPrinterType);}voidprint(Stringtxt){System.out.println(IamInkPrinter);System.out.println(StarttoPrint:+txt);}}classLasertPrinterextendsPrinter{LasertPrinter(StringlaserPrinterType){super(laserPrinterType);}voidprint(Stringtxt){System.out.println(IamLasertPrinter);System.out.println(StarttoPrint:+txt);}}验证用接口实现的多态现象思考与练习:(1)自行设计并编写代码演示面向对象的三大特性(2)练习抽象类的使用(3)练习接口的使用
本文标题:实验二-面向对象程序设计
链接地址:https://www.777doc.com/doc-5267283 .html