java继承与接口实验三
信息与计算科学专业实验报告
课程名称Java课程设计总实验学时: 16 第3 次共6 次
实验项目名称继承与接口本次实验学时数: 3 实验类
型
验证
日期20 12 年 3 月6 日星期二年级
学生姓名学号课任教师
1.实验目的
巩固如下概念:子类的继承性、子类对象的创建过程、成员变量的继承与隐藏、方法的继承与重写;掌握上转型对象的使用;掌握接口回调技术。
2.实验要求
实验前,应事先熟悉相关知识点,拟出相应的实验操作步骤,明确实验目的和要求;实验过程中,服从实验指导教师安排,遵守实验室的各项规章制度,爱护实验仪器设备;实验操作完成后,认真书写实验报告,总结实验经验,分析实验过程中出现的问题。
3.实验内容
1、继承
编写一个Java应用程序,除主类外,该程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople类。要求如下:People类有访问权限是protected的double 型成员变量:height和weight,以及public void speakHello()、public void averageHeight()、public void averageWeight()方法;ChinaPeople类是People的子类,新增了public void chinaGongfu()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()、public void averageWeight()方法;AmericanPeople类是People的子类,新增public void americanBoxing()方法。要求AmericanPeople重写父类的public void speakHello()、public void averageHeight()、public void averageWeight()方法;BeijingPeople类是ChinaPeople的子类,新增public void beijingOpera()方法。要求BeijingPeople重写父类的public void speakHello()、public void averageHeight()、public void averageWeight()方法;请按模板要求,将【代码】替换为Java程序代码。
2、上转型对象
编写一个Java应用程序,要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:public abstract double earnings(); 子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。有一个
Company类,该类用Employee数组作为成员,Employee数组的元素可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。
3、接口回调
卡车要装载一批货物,货物有3种商品:电视、计算机和洗衣机。需要计算出大货车和小货车各自所装载的3中货物的总重量。编写一个Java应用程序,要求有一个ComputeWeight 接口,该接口中有一个方法:public double computeWeight(); 有3个实现该接口的类:Television、Computer和WashMachine。这3个类通过实现接口ComputeWeight给出自重。有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的元素就可以存放Television对象的引用、Computer对象的引用或WashMachine对象的引用。程序能输出Car对象所装载的货物的总重量。
4.实验步骤、实施过程、关键代码、实验结果及分析说明等
(1)代码:
class People
{
protected double weight,height;
public void speakHello()
{
System.out.println("yayawawa");
}
public void averageHeight()
{
height=173;
System.out.println("average height:"+height);
}
public void averageWeight()
{
weight=70;
System.out.println("average weight:"+weight);
}
}
class ChinaPeople extends People
{
public void speakHello()
{
System.out.println("你好,睡了吗?");
}//【代码1】重写public void speakHello()方法
public void averageHeight()
{
System.out.println("中国人的平均身高:168.78厘米");
}//【代码2】重写public void averageHeight()方法
public void averageWeight()
{
System.out.println("中国人的平均体重:65公斤");
}//【代码3】重写public void averageWeight()方法
public void chinaGongfu()
{
System.out.println("坐如钟,站如松,睡如弓.");//【代码4】输出中国武术的信息 }
}
class AmericanPeople extends People
{
public void speakHello()
{
System.out.println("How do you do");
} //【代码5】重写public void speakHello()方法
public void averageHeight()
{
System.out.println("美国人平均身高为175厘米");
}//【代码6】重写public void averageHeight()方法
public void averageWeight()
{
System.out.println("美国人平均体重为75公斤");
} //【代码7】重写public void averageWeight()方法
public void americanBoxing()
{
System.out.println(" 直拳、钩拳.");//【代码8】输出拳击的信息
}
}
class BeijingPeople extends ChinaPeople
{
public void speakHello()
{
System.out.println("你好!");
}//【代码9】重写public void speakHello()方法
public void averageHeight()
{
System.out.println("北京人平均身高为170厘米");
}//【代码10】重写public void averageHeight()方法
public void averageWeight()
{
System.out.println("北京人平均体重为65公斤");
}//【代码11】重写public void averageWeight()方法
public void beijingOpera()
{
System.out.println("京剧的形成大约有150年左右.");//【代码12】输出京剧的信息 }
}
public class Example3.1
{
public static void main(String args[])
{
ChinaPeople chinaPeople=new ChinaPeople();
AmericanPeople americanPeople=new AmericanPeople(); BeijingPeople beijingPeople=new BeijingPeople();
chinaPeople.speakHello();
americanPeople.speakHello();
beijingPeople.speakHello();
chinaPeople.averageHeight();
americanPeople.averageHeight();
beijingPeople.averageHeight();
chinaPeople.averageWeight();
americanPeople.averageWeight();
beijingPeople.averageWeight();
chinaPeople.chinaGongfu();
americanPeople.americanBoxing();
beijingPeople.beijingOpera() ;
beijingPeople.chinaGongfu();
}
}实验结果:
(2)代码:
abstract class Employee
{
double salary;
public abstract double earnings();//返回年收入}
class YearWorker extends Employee
{
public double earnings()
{
return(4000);
}//重写earnings()方法
}
class MonthWorker extends Employee
{
public double earnings()
{
return(3000);
}//重写earnings()方法。
}
class WeekWorker extends Employee
{
public double earnings()
{
return(2000);
}//重写earnings()方法。
}
class Company
{
Employee[] employee;
double salaries=0;
Company(Employee[] employee)
{
this.employee=employee;
}
public double salariesPay()
{
salaries=0;
for(int i=0;i salaries=salaries+employee[i].earnings(); //计算salaries。 return salaries; } } public class HardWork { public static void main(String args[]) { Employee[] employee=new Employee[20]; for(int i=0;i { if(i%3==0) employee[i]=new WeekWorker(); else if(i%3==1) employee[i]=new MonthWorker(); else if(i%3==2) employee[i]=new YearWorker(); } Company company=new Company(employee); System.out.println("公司年工资总额:"+company.salariesPay()); } } 实验结果: (3)代码: interface ComputerWeight { public double computerWeight(); } class Television implements ComputerWeight { public double computerWeight() { return 155; } } class Computer implements ComputerWeight { public double computerWeight() { return 105; } } class WashMachine implements ComputerWeight { public double computerWeight() { return 80; } } class Car { ComputerWeight[] goods; double totalWeights=0; Car(ComputerWeight[] goods) {this.goods=goods; } public double getTotalWeights() { totalWeights=0; for(int i=0;i { totalWeights+=goods[i].computerWeight(); } return totalWeights; } } public class Road { public static void main(String args[]) { ComputerWeight[] goodsOne=new ComputerWeight[50], goodsTwo=new ComputerWeight[22]; for(int i=0;i { if(i%3==0) goodsOne[i]=new Television(); else if(i%3==1) goodsOne[i]=new Computer(); else if(i%3==2) goodsOne[i]=new WashMachine(); } for(int i=0;i { if(i%3==0) goodsTwo[i]=new Television(); else if(i%3==1) goodsTwo[i]=new Computer(); else if(i%3==2) goodsTwo[i]=new WashMachine(); } Car bigcar=new Car(goodsOne); System.out.println("大货车装载的货物重量:"+bigcar.getTotalWeights()); Car smallcar=new Car(goodsTwo); System.out.println("小货车装载的货物重量:"+smallcar.getTotalWeights()); } } 实验结果: 5.小结、体会或建议: JAVA不支持多重继承,子类只能有一个父类。类声明中,用关键字extends来声明一个类是另外一个类的子类。子类和父类在同一个包中,子类自然继承了其父类中不是private的成员变量和方法作为自己的,访问权限不变。不在同一个包中,子类继承父类的protecded,public 成员变量和方法,子类不能继承父类的友好变量和方法。 学生签名: 20 12 年 3 月 6 日 6.教师评语: 成绩程序基本正确,基本完成实验的要求;实验报告格式较规范。 教师签名: 20 12 年 3 月 22日