1、实验三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、统的环境变量设置;2. 确认是否已绿色安装Eclipse集成开发环境。(2) 实验内容一:使用继承和方法覆盖创建子类,并能够创建异类集合并使用多态具体要求:1. 在上述银行项目中,创建Customer类:位于包:com.mybank.domain中;向Customer类添加四个实例变量:firstName,lastName,accounts(Account对象数组,长度为10)和numberOfAccounts(记录accounts数组索引的一个整数);添加一个公有构造方法:两个形式参数firstName,lastname,用于初始化客户姓名、创建accounts数组和初始化accounts数
3、组元素个数numberOfAccounts;添加getFirstName方法:该方法返回客户的firstName实例变量;添加getLastName方法:该方法返回客户的lastName实例变量;添加addAccount方法:该方法有一个形式参数(即Account对象),数组的下标通过实例变量numberOfAccounts实现递增,同时将该参数对象存储在accounts数组中;添加getNumberOfAccounts方法:该方法返回numberOfAccounts实例变量;添加getAccount方法:该方法返回与指定的index数组下标相关的帐户对象。2. 创建SavingsAccoun
4、t类,该类是Account类的子类:位于包:com.mybank.domain中;向SavingsAccount类中添加interestRate实例变量;添加一个有两个参数的公有构造方法:initBalance和interestRate。调用父类构造方法传递initBalance参数,并初始化实例变量interestRate;添加accumulateInterest方法:用于计算客户的利息。3. 创建CheckingAccount类,Account类的子类:位于包:com.mybank.domain中;向CheckingAccount类中添加overdraftAmount实例变量;添加一个有两
5、个参数的公有构造方法:initBalance和overdraftAmount。调用父类构造方法传递initBalance参数,并初始化实例变量overdraftAmount;添加只有一个参数initBalance的另一个公有构造方法。用initBalance参数和overdraftAmount参数调用第一个构造方法,其中overdraftAmount参数使用默认值0.0;覆盖withdraw方法,方法参数为amount,balance变量继承于父类。下面是withdraw方法的伪码: if (balance amount) then double overdraftNeeded = amoun
6、t balance if (overdraftAmount 0) balance = balance + amt; return true; else return false; public boolean withdraw(double amt) / withdraw(double amt),用于从帐户取钱 if (balance amt) balance = balance - amt; return true; else return false; public double getBalance() / getBalance(),用于返回balance的值 return balanc
7、e; Customer1.java:package com.mybank.domain;public class Customer1 / 创建Customer1类 private String firstname; private String lastname; private Account accouts = new Account10; private int numberofaccout; public Customer1(String firstname, String lastname) / 公有构造方法 this.firstname = firstname; this.last
8、name = lastname; numberofaccout = 0; public String getFirstName() / getFirstName方法:该方法返回客户的firstName实例变量; return firstname; public String getLastName() / getLastName方法:该方法返回客户的lastName实例变量; return lastname; public void addaccout(Account accout) / addAccount方法 accoutsnumberofaccout = accout; numberof
9、accout+; public int getnumberofaccout() / getNumberOfAccounts方法:该方法返回numberOfAccounts实例变量 return numberofaccout; public Account getaccout(int index) / getAccount方法:该方法返回与指定的index数组下标相关的帐户对象。 return accoutsindex; Savingaccout.java:package com.mybank.domain;public class Savingaccout extends Account /
10、创建SavingsAccount类,该类是Account类的子类: private double interestrate;/ 添加interestRate实例变量; public Savingaccout(double balance, double interestrate) / 公有构造方法 super.initBalance(balance);/ 调用父类构造方法传递initBalance参数 this.interestrate = interestrate; public double accumulateinterest() / accumulateInterest方法:用于计算客
11、户的利息。 return this.getBalance() * interestrate; Checkingaccout.java:package com.mybank.domain;public class Checkingaccout extends Account / 创建CheckingAccount类,Account类的子类: private double overdraftamount; public Checkingaccout(int balance, double overdraftamount) / 有两个参数的公有构造方法 super.initBalance(balan
12、ce); this.overdraftamount = overdraftamount; public Checkingaccout(int balance) / 只有一个参数initBalance的另一个公有构造方法 this(balance, 0.0); public boolean withdraw(double amount) / 覆盖withdraw方法 if (this.getBalance() amount) double overdraftneeded = amount - this.getBalance(); if (overdraftamount overdraftneed
13、ed) return false; else this.initBalance(this.getBalance() - amount); else this.initBalance(this.getBalance() - amount); return true; Testingbank1.java:package com.mybank.test;import com.mybank.domain.*;public class Testingbank1 public static void main(String args) / 创建TestBanking类 /* 创建异类集合 */ Custo
14、mer1 customer = new Customer1(zhao, zhen); customer.addaccout(new Checkingaccout(500, 100); customer.addaccout(new Savingaccout(500, 0.02); /* 使用instanceof运算符测试判断账户的类型信息 */ for (int i = 0; i customer.getnumberofaccout(); i+) if (customer.getaccout(i) instanceof Checkingaccout) Checkingaccout m = (Ch
15、eckingaccout) customer.getaccout(i); m.withdraw(550); m.deposit(100); m.withdraw(900); m.deposit(100); if (customer.getaccout(i) instanceof Savingaccout) Savingaccout m = (Savingaccout) customer.getaccout(i); m.accumulateinterest(); m.deposit(100); m.withdraw(50); /* 最后输出客户账户的余额信息。 */ for (int i = 0
16、; i customer.getnumberofaccout(); i+) System.out.println(customer.getaccout(i).getBalance(); 实验三2:实验结果:实验改进之处:创建Sleep接口拓展Pet接口。Animal.java:public abstract class Animal / 创建Animal类,该类是抽象类: protected int legs; public Animal() protected Animal(int legs) this.legs = legs; abstract void eat();/ 抽象方法eat;
17、public void walk() / 具体方法walk来显示与动物行走方式有关的信息(包括腿的数目); System.out.println(this animal walk on + legs + legs); public interface Pet / 创建Pet接口:声明三个抽象方法 public abstract String getname(); public abstract void setname(String pet_name); public abstract void play();public interface Sleep extends Pet/创建Sleep
18、接口,继承Pet void sleep();Spider.java:public class Spider extends Animal implements Pet,Sleep/创建Spider类,Spider类扩展了Animal类 private String pet_name; public Spider() super(8); void eat()/实现eat方法 System.out.println(Spider eat fly); public void setname(String pet_name) this.pet_name=pet_name; public void pla
19、y() System.out.println(the spider play in hands); public String getname() return pet_name; public void speak() System.out.println(the cat makes no noise); public void sleep()/实现Sleep接口方法 System.out.println(the cat sleep on the wall); Cat.java:public class Cat extends Animal implements Pet,Sleep/创建Ca
20、t类 private String pet_name; public Cat(String pet_name)/构造方法 super(4); this.pet_name=pet_name; public Cat()/定义一个无参数的构造方法 this(null); public void speak() System.out.println(the cat speak with maomao); void eat()/实现eat方法; System.out.println(the cats like eatting fish); /*实现Pet接口方法*/ public void play()
21、 System.out.println(the cat play with mice); public String getname() return pet_name; public void setname(String pet_name) this.pet_name=pet_name;public void sleep()/实现Sleep接口方法 System.out.println(the cat sleep with her host);Fish.java:public class Fish extends Animal implements Pet,Sleep/创建Fish类 pr
22、ivate String pet_name; public Fish() super(0); /*实现Pet接口方法*/ public String getname() return pet_name; public void setname(String pet_name) this.pet_name=pet_name; public void play() System.out.println(the fish swim in water); void eat()/实现eat方法 System.out.println(the fish eat rice); public void walk
23、()/覆盖walk方法 System.out.println(this animal does not have legs); public void speak() System.out.println(the fish makes no noise); public void sleep()/实现Sleep接口方法 System.out.println(the fish sleep with water); TestAnimal.java:public class TestAnimal /创建TestAnimal类 public static void main(String args)
24、Pet pet=new Pet3; pet0=new Cat();/创建并操作前面所创建的类的实例 pet1=new Fish(); pet2=new Spider(); for(Pet i:pet)/调用每个对象中的方法 i.play(); Animal animal=(Animal) i; /对象类型转换,使用多态特性 animal.walk(); Sleep sleep=(Sleep)i; sleep.sleep(); for(int i=0;i3;i+)/使用instanceof,向下转型 if(peti instanceof Cat ) Cat m=(Cat) peti; m.speak(); if(peti instanceof Fish ) Fish m=(Fish) peti; m.speak(); if(peti instanceo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1