继承与接口实验.doc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 继承 接口 实验
- 资源描述:
-
深 圳 大 学 实 验 报 告 课程名称: 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、数组可以用来存放对象和的引用。展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




继承与接口实验.doc



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/12004575.html