收藏 分销(赏)

继承与接口实验.doc

上传人:仙人****88 文档编号:12004575 上传时间:2025-08-26 格式:DOC 页数:10 大小:86.54KB 下载积分:10 金币
下载 相关 举报
继承与接口实验.doc_第1页
第1页 / 共10页
继承与接口实验.doc_第2页
第2页 / 共10页


点击查看更多>>
资源描述
深 圳 大 学 实 验 报 告 课程名称: Java 实验序号: 实验2 实验名称: 继承与接口 班 级: 计算机3 姓 名: 卢志敏 同 组 人: 实验日期: 2008 年 11 月 16 日 教师签字: 一、实验目的 1.继承 l 子类的继承性 l 子类对象的创建过程 l 成员变量的继承与隐藏 l 方法的继承与重写 2. 上转型对象 掌握上转型对象的使用 3. 接口回调 掌握接口回调技术 二、实验环境 JDK1.5 Winxp 三、实验步骤与方法 实验1 编写一个Java应用程序,除了主类外,该程序中还有4个类:People、ChinaPeople、AmericanPeople类。该程序具体要求如下: l People类有访问权限是protected的double型变量:height和weight,以及public void speakHello()、public void averageHeight() 和public void averageWeight()方法。 l ChinaPeople类是People的子类,新增了public void chinaGongfu()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。 l AmericanPeople类是People的子类,新增public void americanBoxing()方法。要求AmericanPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。 l BeijingPeople是ChinaPelple的子类,新增public void beijingOpera()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。 实验2 要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有个abstract方法: Public abstract earnings(); 子类必须重写父类的earnings()方法,给出各自领取薪水的具体方式。 有一个Company类,该类用Employee数组作为成员,Employee数组的单元可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象、WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。 实验3 卡车要装载一批货物,货物有3种商品:电视、计算机和洗衣机。需要计算出大货车和小货车各自做载重的3种货物的总重量。 要求有一个ComputerWeight()接口,该接口中有一个方法: Public double computeWeight() 有3个实现该接口的类:Telvision、Computer和WashMachine。这3类通过实现接口computerTotalSales给出自重。 有一个Car类,该类用ComputeWeight接口类型的数组作为成员,那么该数组的单元就可以存放Television对象的引用、Computer对象的引用或WashMachine对象引用。程序能输出Car对象所能装载的货物的总重量。 按程序模板的要求编写源文件,要特别注意程序的输出结果,并能正确解释输出的结果。 四、结果与分析 实验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("你好,吃了吗"); } public void averageHeight(){ System.out.println("中国人的平均身高:173.0厘米"); } public void averageWeight(){ System.out.println("中国人的平均体重:67.34公斤"); } public void chinaGongfu() { System.out.println("坐如钟,站如松,睡如弓"); } } class AmericanPeople extends People { public void speakHello(){ System.out.println("How do you do"); } public void averageHeight(){ System.out.println("American Average height: 188.0 cm"); } public void averageWeight(){ System.out.println("American Average weight: 80.23 kg"); } public void americanBoxing() { System.out.println("直拳 钩拳"); } } class BeijingPeople extends ChinaPeople { public void speakHello(){ System.out.println("您好"); } public void averageHeight(){ System.out.println("北京人的平均身高: 167.0 cm"); } public void averageWeight(){ System.out.println("北京人的平均体重: 68.5 kg"); } public void beijingOpera() { System.out.println("京剧术语"); } } public class Example { 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 { public abstract double earnings( ); } class YearWorker extends Employee { public double earnings( ){ return 50000 ; } } class MonthWorker extends Employee { public double earnings( ){ return 12*2300; } } class WeekWorker extends Employee { public double earnings( ){ return 48*500; } } class Company { Employee[] employee; double salaries; Company (Employee[] employee) { this.employee=employee; } public double salariesPay() { salaries=0; for(int i=0;i<employee.length;i++) { salaries=salaries+employee[i].earnings(); } return salaries; } } public class HardWork { public static void main(String args[]) { Employee[] employee=new Employee[20]; for(int i=0;i<employee.length;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());//调用Company对象的方法输出工资总额 } } 运行截屏: 实验3 源代码:interface ComputerWeight { public double computeWeight(); } class Television implements ComputerWeight { public double computeWeight() { return 10; //实现computeWeight()方法 } } class Computer implements ComputerWeight { public double computeWeight() { return 15; //实现computeWeight()方法 } } class WashMachine implements ComputerWeight { public double computeWeight() { return 30.5; //实现computeWeight()方法 } } class Car { ComputerWeight[] goods; double totalWeights=0; Car(ComputerWeight[] goods) { this.goods=goods; } public double getTotalWeights() { totalWeights=0; for(int i=0;i<goods.length;i++) { totalWeights+=goods[i].computeWeight(); } 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<goodsOne.length;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<goodsTwo.length;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 大货车=new Car(goodsOne); System.out.println("大货车的装载货物重量:"+大货车.getTotalWeights()); Car 小货车=new Car(goodsTwo); System.out.println("小货车的装载货物重量:"+小货车.getTotalWeights()); } } 运行效果截屏: 五、思考题解答 实验1 就本程序而言,People类中的 public void speakHello() public void averageHeight() Public void averageWeight() 三个方法的方法体中的语句是否可以省略。 解答:可以省略,子类会重写父类的方法,所以本程序中父类方法的方法体可以省略。 实验2 子类YearWorker如果不重写earnings方法,程序编译时提示怎样的错误? 解答:将提示这样的错误:YearWorker is not abstract and dose not override abstract method earnings() in Employee 即没有重写父类的方法。 实验3 请在实验的基础上再编写一个实现ComputerWeight接口的类,比如Refrigerrator。这样一来,大货车或小货车装载的货物中就可以有Refrigerrator类型的对象。 增加一个实现ComputerWeight接口的类后,Car类需要进行修改吗? 解答:新增一个Rerfigerrator类: class Rerfigerrator implements ComputerWeight { public double computeWeight() { return 40.5; //实现computeWeight()方法 } } 而Car类并不需要修改,因为Car类只是引用了商品的首地址,而具体每个商品是什么,Car类并不关心。 六、心得体会 1、方法重写时一定要保证方法的名字、类型、参数个数和类型同父类的某个方法完全相同,只有这样,子类继承的这个方法才被隐藏。 2、尽管abstract类不能创建对象,但abstract类声明的对象可以存放子类对象的引用,即成为子类对象的上转型对象。 3、数组可以用来存放对象和的引用。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服