Java实验七, 接口与多态

匿名 (未验证) 提交于 2019-12-02 21:35:18

一. 实验目的

1. 理解接口与多态;

2. 掌握接口回调技术;

3. 掌握面向接口编程思想。

二. 实验内容及要求

卡车要装载一批货物,货物由电视机、计算机和洗衣机组成,卡车需要计算出整批货物的重量。编写能够满足如下条件的程序:

1. 定义一个接口,包含计算货物重量的抽象方法。

2. 分别定义实现上述接口的用于计算电视机、计算机和洗衣机的类。

3. 定义一个卡车类,在其中定义一个数组成员变量表示其装载的货物,并提供能计算所载整批货物重量的方法。

4. 在主类中模拟卡车装载N件货物,每件货物的类别随机,输出其所载货物的总重量。

三. 实验过程及结果

 Goods.java public interface Goods { 	double cal_weight();//计算货物重量的抽象方法 } 
 TV.java public class TV implements Goods{ 	double singleTVWeight;//单个电视机重量 	double single_weight[] = {5.5, 3.1, 4.1, 6};//可选择的电视机重量  	public TV(int randomInt) { 		this.singleTVWeight = single_weight[randomInt]; 	}//构造方法,从single_weight[]中随机选中一个重量并设置  	public double cal_weight() { 		return singleTVWeight; 	}//计算电视机重量的方法,返回电视机重量 } 
 Computer.java public class Computer implements Goods{ 	double singleComputerWeight;//单个电脑重量 	double single_weight[] = {1.5, 1.9, 2.0, 2.4};//可选择的电脑重量  	public Computer(int randomInt) { 		this.singleComputerWeight = single_weight[randomInt]; 	}//构造方法,从single_weight[]中随机选中一个重量并设置  	public double cal_weight() { 		return singleComputerWeight; 	}//计算电脑重量的方法,返回电脑重量 } 
 WashingMachine.java public class WashingMachine implements Goods{ 	double singleWMWeight;//单个洗衣机重量 	double single_weight[] = {13.5, 11.9, 20, 12.4};//可选择的洗衣机重量  	public WashingMachine(int randomInt) { 		this.singleWMWeight = single_weight[randomInt]; 	}//构造方法,从single_weight[]中随机选中一个重量并设置  	public double cal_weight() { 		return singleWMWeight; 	}//计算洗衣机重量的方法,返回洗衣机重量 } 
 Truck.java import java.util.Random;  public class Truck { 	static Goods[] goods;//数组成员变量,表示其装载的货物 	 	static void show_weight(Goods[] goods) { 		double sum = 0;//货物总重,初始为0  		for (Goods g: goods) { 			sum += g.cal_weight(); 		}//计算货物总重 		 		System.out.printf("卡车装载货物的总重量为:%.2fkg\n", sum); 	} 	 	public static void main(String[] args) { 		Random random = new Random();//创建随机数生成器random 		Truck.goods = new Goods[50];//设置N为50  		//计算电脑、洗衣机、电视机总数 		int countTV = 0, countWM = 0, countCom = 0; 		//计算电脑、洗衣机、电视机总重 		double TVWeight = 0, WMWeight = 0, ComWeight = 0; 		 		for (int i = 0; i < Truck.goods.length; i++) { 			int selection = random.nextInt(3); 			int index = random.nextInt(4); 			 			if (selection == 0) {//若随机数为0,货物种类为“电脑” 				Truck.goods[i] = new Computer(index); //设置数组中第i个货物种类为“电脑” 				countCom++;//电脑总数加1 				ComWeight += Truck.goods[i].cal_weight(); 			} else if (selection == 1) {//若随机数为1,货物种类为“洗衣机” 				Truck.goods[i] = new WashingMachine(index); //设置数组中第i个货物种类为“洗衣机” 				countWM++;//洗衣机总数加1 				WMWeight += Truck.goods[i].cal_weight(); 			} else if (selection == 2) {//若随机数为2,货物种类为“电视机” 				Truck.goods[i] = new TV(index); //设置数组中第i个货物种类为“电视机” 				countTV++;//电视机总数加1 				TVWeight += Truck.goods[i].cal_weight(); 			}  		} 		 		System.out.printf("电脑总数:%d, 总重:%.2fkg\n", countCom, ComWeight); 		System.out.printf("洗衣机总数:%d, 总重:%.2fkg\n", countWM, WMWeight); 		System.out.printf("电视机总数:%d, 总重:%.2fkg\n", countTV, TVWeight); 		System.out.println("卡车装载货物的总数为:" + Truck.goods.length); 		Truck.show_weight(Truck.goods); 	} }	 

四. 实验中的问题及心得

1. 通过此次实验我对面向接口编程有了更深的了解:

(1)面向接口编程要遵循开-闭原则,使程序对接口的修改“关闭”,否则一旦修改接口(比如增加一个abstract方法),则实现该接口的类都要修改;对增加实现接口的类开放,这样可以使程序的功能易于扩充

(2)java只允许单继承,每个子类只能有一个父类;而使用接口,每个类却可以实现多个接口

2. 此次实验中,既使用了接口回调,也使用了对象数组,我对对象数组也有了更深的理解。比如:

(1)A[] a;

(2)a = new A[10];

(3)a[i] = new A();

前两步只是定义了数组a有10个元素,每个元素都是A类的对象,这些对象目前都是空对象;第三步后,数组中每个元素才是真正的对象

文章来源: https://blog.csdn.net/weixin_42250302/article/details/89293175
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!