实验三Java面向对象高级编程.docx

上传人:b****4 文档编号:4998929 上传时间:2022-12-12 格式:DOCX 页数:15 大小:73.17KB
下载 相关 举报
实验三Java面向对象高级编程.docx_第1页
第1页 / 共15页
实验三Java面向对象高级编程.docx_第2页
第2页 / 共15页
实验三Java面向对象高级编程.docx_第3页
第3页 / 共15页
实验三Java面向对象高级编程.docx_第4页
第4页 / 共15页
实验三Java面向对象高级编程.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

实验三Java面向对象高级编程.docx

《实验三Java面向对象高级编程.docx》由会员分享,可在线阅读,更多相关《实验三Java面向对象高级编程.docx(15页珍藏版)》请在冰豆网上搜索。

实验三Java面向对象高级编程.docx

实验三Java面向对象高级编程

实验三Java面向对象高级编程

一、实验目的

1.掌握Java面向对象编程技术

2.能够使用继承创建子类并实现方法覆盖

3.能够创建异类集合并使用多态

4.能够创建抽象类和接口,并探究它们的多态特性

二、预备知识

1.JDK的安装设置:

JDK/JRE/JVM;

2.Eclipse集成开发环境的绿色安装;

3.掌握Java语言的面向对象特性(封装性、继承性、多态性);

4.掌握Java语言的抽象类和接口。

三、实验描述

1.实验类型:

设计

2.实验学时:

4学时

3.实验内容:

2项(参照四)

四、实验内容

 

(1)检查所使用的计算机系统:

1.确认是否已安装JDK,并确认系统的环境变量设置;

2.确认是否已绿色安装Eclipse集成开发环境。

 

(2)实验内容一:

使用继承和方法覆盖创建子类,并能够创建异类集合并使用多态

具体要求:

1.在上述银行项目中,创建Customer类:

·位于包:

com.mybank.domain中;

·向Customer类添加四个实例变量:

firstName,lastName,accounts(Account对象数组,长度为10)和numberOfAccounts(记录accounts数组索引的一个整数);

·添加一个公有构造方法:

两个形式参数firstName,lastname,用于初始化客户姓名、创建accounts数组和初始化accounts数组元素个数numberOfAccounts;

·添加getFirstName方法:

该方法返回客户的firstName实例变量;

·添加getLastName方法:

该方法返回客户的lastName实例变量;

·添加addAccount方法:

该方法有一个形式参数(即Account对象),数组的下标通过实例变量numberOfAccounts实现递增,同时将该参数对象存储在accounts数组中;

·添加getNumberOfAccounts方法:

该方法返回numberOfAccounts实例变量;

·添加getAccount方法:

该方法返回与指定的index数组下标相关的帐户对象。

2.创建SavingsAccount类,该类是Account类的子类:

·位于包:

com.mybank.domain中;

·向SavingsAccount类中添加interestRate实例变量;

·添加一个有两个参数的公有构造方法:

initBalance和interestRate。

调用父类构造方法传递initBalance参数,并初始化实例变量interestRate;

·添加accumulateInterest方法:

用于计算客户的利息。

3.创建CheckingAccount类,Account类的子类:

·位于包:

com.mybank.domain中;

·向CheckingAccount类中添加overdraftAmount实例变量;

·添加一个有两个参数的公有构造方法:

initBalance和overdraftAmount。

调用父类构造方法传递initBalance参数,并初始化实例变量overdraftAmount;

·添加只有一个参数initBalance的另一个公有构造方法。

用initBalance参数和overdraftAmount参数调用第一个构造方法,其中overdraftAmount参数使用默认值0.0;

·覆盖withdraw方法,方法参数为amount,balance变量继承于父类。

下面是withdraw方法的伪码:

if(balance

then

doubleoverdraftNeeded=amount–balance

if(overdraftAmount

thentransactionfails

else

balance=0.0

decrementoverdraftAmountbyoverdraftNeeded

else

decrementbalancebyamount

4.创建TestBanking类:

·该类文件位于包:

com.mybank.test

·该类有程序入口main()函数;

·要求:

创建一名客户的若干个不同类型的账户信息,然后依次访问每一个账户,并根据不同类型的账户信息做出不同的操作。

(要求使用instanceof运算符测试判断账户的类型信息,如果是SavingsAccount,则计算账户的利息;如果是CheckingAccount,则进行取钱操作,可以进行适当额度的透支);

·最后输出客户账户的余额信息。

 (4)实验内容二:

能够创建抽象类和接口,并探究它们的多态特性

具体要求:

1.创建项目:

InterfaceProject;

2.创建Animal类,该类是抽象类:

·声明一个受保护的整数实例变量legs,记录动物的腿的数目;

·定义一个受保护的构造方法来初始化legs实例变量;

·声明抽象方法eat;

·声明具体方法walk来显示与动物行走方式有关的信息(包括腿的数目);

3.创建Pet接口:

声明三个抽象方法:

publicStringgetName();

publicvoidsetName(Stringn);

publicvoidplay();

4.创建Spider类:

·Spider类扩展了Animal类;

·定义一个无参数构造方法,调用父类构造方法来指明所有蜘蛛都有八条腿;

·实现eat方法;

5.创建Cat类:

·声明String实例变量来存储宠物的名字;

·定义一个构造方法,使用String参数来指定猫的名字;该构造方法必须调用父类构造方法来指明所有猫都有四条腿;

·另外定义一个无参数的构造方法,该构造方法调用上一个构造方法(使用this关键字)来传递一个空字符串作为参数;

·实现Pet接口方法;

·实现eat方法;

6.创建Fish类:

·声明String实例变量来存储宠物的名字;

·定义一个无参数的构造方法,该构造方法调用父类构造方法来指明鱼没有腿;

·实现Pet接口方法;

·覆盖walk方法,该方法调用所有的超级方法,并打印输出一条说明鱼不会行走的消息;

·实现eat方法;

7.创建TestAnimal类:

·该类有程序入口main()函数;

·创建并操作前面所创建的类的实例;

·调用每个对象中的方法;

·对象类型转换;

·使用多态特性;

·使用super关键字调用父类方法。

五、实验要求及总结

1.结合上课内容,对上述程序先阅读,然后上机并调试程序,并对实验结果写出你自己的分析结论。

2.整理上机步骤,总结经验和体会。

3.完成实验报告和上交程序。

实验三1:

实验结果:

Account.java:

packagecom.mybank.domain;

importcom.mybank.test.*;

publicclassAccount{//创建Account类

privatedoublebalance;

publicvoidinitBalance(doublebalance){

this.balance=balance;

}

publicbooleandeposit(doubleamt){//  deposit(double

//amt),用于向帐户存钱,返回值是boolean型;

if(amt>0){

balance=balance+amt;

returntrue;

}else{

returnfalse;

}

}

publicbooleanwithdraw(doubleamt){//withdraw(doubleamt),用于从帐户取钱

if(balance>amt){

balance=balance-amt;

returntrue;

}else{

returnfalse;

}

}

publicdoublegetBalance(){//getBalance(),用于返回balance的值

returnbalance;

}

}

Customer1.java:

packagecom.mybank.domain;

publicclassCustomer1{//创建Customer1类

privateStringfirstname;

privateStringlastname;

privateAccountaccouts[]=newAccount[10];

privateintnumberofaccout;

publicCustomer1(Stringfirstname,Stringlastname){//公有构造方法

this.firstname=firstname;

this.lastname=lastname;

numberofaccout=0;

}

publicStringgetFirstName(){//getFirstName方法:

该方法返回客户的firstName实例变量;

returnfirstname;

}

publicStringgetLastName(){//  getLastName方法:

该方法返回客户的lastName实例变量;

returnlastname;

}

publicvoidaddaccout(Accountaccout){//addAccount方法

accouts[numberofaccout]=accout;

numberofaccout++;

}

publicintgetnumberofaccout(){//getNumberOfAccounts方法:

该方法返回numberOfAccounts实例变量

returnnumberofaccout;

}

publicAccountgetaccout(intindex){//  getAccount方法:

该方法返回与指定的index数组下标相关的帐户对象。

returnaccouts[index];

}

}

Savingaccout.java:

packagecom.mybank.domain;

publicclassSavingaccoutextendsAccount{//创建SavingsAccount类,该类是Account类的子类:

privatedoubleinterestrate;//  添加interestRate实例变量;

publicSavingaccout(doublebalance,doubleinterestrate){//公有构造方法

super.initBalance(balance);//调用父类构造方法传递initBalance参数

this.interestrate=interestrate;

}

publicdoubleaccumulateinterest(){//accumulateInterest方法:

用于计算客户的利息。

returnthis.getBalance()*interestrate;

}

}

Checkingaccout.java:

packagecom.mybank.domain;

publicclassCheckingaccoutextendsAccount{//创建CheckingAccount类,Account类的子类:

privatedoubleoverdraftamount;

publicCheckingaccout(intbalance,doubleoverdraftamount){//有两个参数的公有构造方法

super.initBalance(balance);

this.overdraftamount=overdraftamount;

}

publicCheckingaccout(intbalance){//只有一个参数initBalance的另一个公有构造方法

this(balance,0.0);

}

publicbooleanwithdraw(doubleamount){//覆盖withdraw方法

if(this.getBalance()

doubleoverdraftneeded=amount-this.getBalance();

if(overdraftamount

returnfalse;

}else{

this.initBalance(this.getBalance()-amount);

}

}else{

this.initBalance(this.getBalance()-amount);

}

returntrue;

}

}

Testingbank1.java:

packagecom.mybank.test;

importcom.mybank.domain.*;

publicclassTestingbank1{

publicstaticvoidmain(String[]args){//创建TestBanking类

/*创建异类集合*/

Customer1customer=newCustomer1("zhao","zhen");

customer.addaccout(newCheckingaccout(500,100));

customer.addaccout(newSavingaccout(500,0.02));

/*使用instanceof运算符测试判断账户的类型信息*/

for(inti=0;i

if(customer.getaccout(i)instanceofCheckingaccout){

Checkingaccoutm=(Checkingaccout)customer.getaccout(i);

m.withdraw(550);

m.deposit(100);

m.withdraw(900);

m.deposit(100);

}

if(customer.getaccout(i)instanceofSavingaccout){

Savingaccoutm=(Savingaccout)customer.getaccout(i);

m.accumulateinterest();

m.deposit(100);

m.withdraw(50);

}

}

/*最后输出客户账户的余额信息。

*/

for(inti=0;i

System.out.println(customer.getaccout(i).getBalance());

}

}

}

实验三2:

实验结果:

实验改进之处:

创建Sleep接口拓展Pet接口。

Animal.java:

publicabstractclassAnimal{//创建Animal类,该类是抽象类:

protectedintlegs;

publicAnimal(){

}

protectedAnimal(intlegs){

this.legs=legs;

}

abstractvoideat();//抽象方法eat;

publicvoidwalk(){//具体方法walk来显示与动物行走方式有关的信息(包括腿的数目);

System.out.println("thisanimalwalkon"+legs+"legs");

}

}

publicinterfacePet{//创建Pet接口:

声明三个抽象方法

publicabstractStringgetname();

publicabstractvoidsetname(Stringpet_name);

publicabstractvoidplay();

}

publicinterfaceSleepextendsPet{//创建Sleep接口,继承Pet

voidsleep();

}

Spider.java:

publicclassSpiderextendsAnimalimplementsPet,Sleep{//创建Spider类,Spider类扩展了Animal类

privateStringpet_name;

publicSpider(){

super(8);

}

voideat(){//实现eat方法

System.out.println("Spidereatfly");

}

publicvoidsetname(Stringpet_name){

this.pet_name=pet_name;

}

publicvoidplay(){

System.out.println("thespiderplayinhands");

}

publicStringgetname(){

returnpet_name;

}

publicvoidspeak(){

System.out.println("thecatmakesnonoise");

}

publicvoidsleep(){//实现Sleep接口方法

System.out.println("thecatsleeponthewall");

}

}

Cat.java:

publicclassCatextendsAnimalimplementsPet,Sleep{//创建Cat类

privateStringpet_name;

publicCat(Stringpet_name){//构造方法

super(4);

this.pet_name=pet_name;

}

publicCat(){//定义一个无参数的构造方法

this(null);

}

publicvoidspeak(){

System.out.println("thecatspeakwithmaomao");

}

voideat(){//实现eat方法;

System.out.println("thecatslikeeattingfish");

}

/*实现Pet接口方法*/

publicvoidplay(){

System.out.println("thecatplaywithmice");

}

publicStringgetname(){

returnpet_name;

}

publicvoidsetname(Stringpet_name){

this.pet_name=pet_name;

}

publicvoidsleep(){//实现Sleep接口方法

System.out.println("thecatsleepwithherhost");

}

}

Fish.java:

publicclassFishextendsAnimalimplementsPet,Sleep{//创建Fish类

privateStringpet_name;

publicFish(){

super(0);

}

/*实现Pet接口方法*/

publicStringgetname(){

returnpet_name;

}

publicvoidsetname(Stringpet_name){

this.pet_name=pet_name;

}

publicvoidplay(){

System.out.println("thefishswiminwater");

}

voideat(){//实现eat方法

System.out.println("thefisheatrice");

}

publicvoidwalk(){//覆盖walk方法

System.out.println("thisanimaldoesnothavelegs");

}

publicvoidspeak(){

System.out.println("thefishmakesnonoise");

}

publicvoidsleep(){//实现Sleep接口方法

System.out.println("thefishsleepwithwater");

}

}

TestAnimal.java:

publicclassTestAnimal{//创建TestAnimal类

publicstaticvoidmain(String[]args){

Petpet[]=newPet[3];

pet[0]=newCat();//创建并操作前面所创建的类的实例

pet[1]=newFish();

pet[2]=newSpider();

for(Peti:

pet){//调用每个对象中的方法

i.play();

Animalanimal=(Animal)i;//对象类型转换,使用多态特性

animal.walk();

Sleepsleep=(Sleep)i;

sleep.sleep();

}

for(inti=0;i<3;i++){//使用instanceof,向下转型

if(pet[i]instanceofCat){

Catm=(Cat)pet[i];

m.speak();

}

if(pet[i]instanceofFish){

Fishm=(Fish)pet[i];

m.speak();

}

if(pet[i]instanceo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1