![实验二 继承与接口实验_第1页](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/f73b738e-b6aa-4c76-a081-c246ca27f9d6/f73b738e-b6aa-4c76-a081-c246ca27f9d61.gif)
![实验二 继承与接口实验_第2页](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/f73b738e-b6aa-4c76-a081-c246ca27f9d6/f73b738e-b6aa-4c76-a081-c246ca27f9d62.gif)
![实验二 继承与接口实验_第3页](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/f73b738e-b6aa-4c76-a081-c246ca27f9d6/f73b738e-b6aa-4c76-a081-c246ca27f9d63.gif)
![实验二 继承与接口实验_第4页](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/f73b738e-b6aa-4c76-a081-c246ca27f9d6/f73b738e-b6aa-4c76-a081-c246ca27f9d64.gif)
![实验二 继承与接口实验_第5页](http://file3.renrendoc.com/fileroot_temp3/2022-4/26/f73b738e-b6aa-4c76-a081-c246ca27f9d6/f73b738e-b6aa-4c76-a081-c246ca27f9d65.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验二 继承与接口一、实验目的1. 掌握类的继承机制。2. 熟悉类中成员变量和方法的访问控制。3. 掌握接口与包的使用,熟悉方法的多态性。二、实验内容1. 定义父类及子类,在子类中重写父类的方法2. 练习接口与包的使用三、.实验步骤与要求第1题 继承编写一个Java应用程序,除了主类外,该程序中还有4个类:People, ChinaPeople, AmericanPeople 和 BeijingPeople类。此四个类的继承关系如下图所示:要求ChinaPeople,American类均重写其父类People类的speakHello, averageHeight, averageWeight方
2、法,BeijingPeople类重写其父类ChinaPeople类的speakHello, averageHeight, averageWeight方法。People类 变量 方法protected double height public void speak Hello() protected double weight public void averageHeight() public void averageWeight()ChinaPeople类方法: public void chinaGongfu()AmericanPeople类方法:Public void americanBo
3、xing()BeijingPeople类方法:Public void beijingOpera()源代码:package people;class peopleprotected double height ; protected double weight ; public void speakHello() /问候语的函数System.out.println("hello");public void averageHeight()/人们的平均身高 height=170;System.out.println(+height);public void averageWeig
4、ht()/人们的平均体重 weight=120;System.out.println(+weight);class Chinapeople extends peoplepublic void speakHello() System.out.println("你好");public void averageHeight() height=172;System.out.println(+height);public void averageWeight() weight=115;System.out.println(+weight);public void chinaGongf
5、u()/中国功夫的方法System.out.println("中国功夫");class Americanpeople extends peoplepublic void speakHello() System.out.println("hello");public void averageHeight() height=180;System.out.println(+height);public void averageWeight() weight=150;System.out.println(+weight);public void american
6、Boxing()/美国拳击的方法System.out.println("americanBoxing");class Beijingpeople extends Chinapeoplepublic void speakHello() System.out.println("北京欢迎你");public void averageHeight() height=168;System.out.println(+height);public void averageWeight() weight=125;System.out.println(+weight);c
7、lass Examplepublic static void main(String args)people p =new people();Chinapeople c=new Chinapeople();Americanpeople a=new Americanpeople();Beijingpeople b=new Beijingpeople();p.averageHeight();p.averageWeight();p.speakHello();c.averageHeight();c.averageWeight();c.chinaGongfu();c.speakHello();a.ave
8、rageHeight();a.averageWeight();a.americanBoxing();a.speakHello();b.averageHeight();b.averageWeight();b.speakHello();结果截图:第2题 上转型对象要求有一个abstract类,类名为Employee。Employee的子类有YearWorker,MonthWorker和WeekWorker。YearWorker按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:public abstract earnin
9、gs();子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。有一个Company类,该类用employee数组作为成员,employee数组的单元可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象或WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。源代码:package people; abstract class Employee int salary;public abstract void earnings();class YearWorker extends Employee public v
10、oid earnings() salary=200;System.out.println("年薪:" +salary*365);class MonthWorker extends Employee public void earnings() salary=100;System.out.println("月薪:"+salary*30+" 一年薪水: "+salary*30*12);class WeekWorker extends Employee public void earnings() salary=80;System.out.
11、println("周薪:"+salary*7+" 一年薪水: "+salary*365);public class Company public static void main(String args) Employee e=new Employee3; e0=new YearWorker(); e0.earnings(); e1=new MonthWorker(); e1.earnings(); e2=new WeekWorker(); e2.earnings(); 结果截图:第3题 接口回调要求有一个ComputeTotalSales接口,该接口中
12、有一个方法:public double totalSalesByYear(),有三个实现该接口的类:Television,Computer和Mobile。这三个类通过实现接口computeTotalSales,给出自己的年销售额。有一个Shop类,该类用computeTotalSales数组作为成员,computeTotalSales数组的单元可以存放Television对象的引用、Computer对象的引用或Mobile对象的引用。程序能输出Shop对象的年销售额。源代码:package people;interface ComputeTotalSalespublic double tota
13、lSalesByYear();class Television implements ComputeTotalSalespublic double totalSalesByYear()double sale=100;return sale*365;class computer implements ComputeTotalSalespublic double totalSalesByYear()double sale=200;return sale*365;class Mobile implements ComputeTotalSalespublic double totalSalesByYe
14、ar()double sale=220;return sale*365;public class shop public static void main(String args) ComputeTotalSales c; c=new ComputeTotalSales3; c0=new Television(); System.out.println("电视机的年销售量:"+c0.totalSalesByYear(); c1=new computer(); System.out.println("电脑的年销售量:"+c1.totalSalesByYear(); c2=ne
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 六年级折扣听评课记录
- 教师健康守护
- 首师大版道德与法治七年级上册1.2《融入新集体》听课评课记录
- 2025年度智能交通系统监理服务合同
- 苏科版九年级数学听评课记录:第29讲 圆与圆的位置关系
- 2025年度基础设施建设项目施工合同风险防控策略集锦
- 用故事照亮家庭教育的道路
- 湘教版数学八年级上册2.5《第5课时 全等三角形的判定(SSS)》听评课记录
- 生态文明教育在办公环境中的实践与探索
- 2025年度智能酒店用品绿色采购专项购销合同
- 2025届高考物理二轮总复习第一编专题2能量与动量第1讲动能定理机械能守恒定律功能关系的应用课件
- T型引流管常见并发症的预防及处理
- 2024-2025学年人教新版九年级(上)化学寒假作业(九)
- 内业资料承包合同个人与公司的承包合同
- 【履职清单】2024版安全生产责任体系重点岗位履职清单
- 2022年全国医学博士英语统一考试试题
- 学校工作总结和存在的不足及整改措施
- 《工业自动化技术》课件
- (绩效考核)钳工技能鉴定考核试题库
- 2024年江苏农牧科技职业学院单招职业适应性测试题库参考答案
- 知识图谱与大模型融合实践研究报告
评论
0/150
提交评论