版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026钻石矿和钻石金行业市场现状供需分析及投资评估规划分析研究报告
- 2026中国肌营养不良症基因替代疗法产业化瓶颈突破分析
- 《技术指标杨洋》课件
- 2026工业母机高端化转型技术路线与政策支持
- 2026中国食品饮料行业过程监控可视化解决方案研究报告
- 教科版科学六上电能和能量
- 《控制铁生锈的速度》课件
- 《父母的自恋特征》课件
- 2026中国农业机械化推广现状及区域发展差异报告
- 2026消费级3D打印设备市场教育周期与渠道变革研究报告
- 2026年消防安全专题培训(含近期火灾事故案例)
- AI赋能下初高中理化生跨学科教学创新研究
- 抹灰面层施工安全技术交底
- 肺大疱诊疗专家共识
- 中国冻伤临床诊疗与修复指南(2025版)
- 中华民族共同体课件
- 中国电建安全培训
- 中文修订版儿童社会能力和行为评定量表(SCBE-30)
- 2025秋统编版小学道德与法治三年级上册教学设计(附全册目录)
- IPC7525B2011(CN)Stencildesignguidelines模板设计指南(中文版)
- 临沂工资管理办法
评论
0/150
提交评论