




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 区块链在艺术品收藏与拍卖中的应用考核试卷
- 产业链整合与产业政策协调机制研究考核试卷
- 家电新零售的智能售后服务平台构建考核试卷
- 移动端产品设计与用户体验优化考核试卷
- 农村社会安全与农村社会动员机制研究考核试卷
- 新疆法宣考试试题及答案
- 乡村工匠测试题及答案
- 华为上海java开发社招面试题及答案
- 城投控股笔试题及答案
- 通信安全员c类考试试题及答案
- 2024年四川省甘孜县林业局公开招聘试题带答案详解
- 2025-2030中国转轮除湿机行业前景动态及投资规划分析报告
- 2025年河南省中考道德与法治真题含答案
- 2025年人教版小学五年级语文(下册)期末试卷附答案
- 中国人民警察学院面试内容与回答
- 2025年事业单位公开招聘考试《综合应用能力(E类)西医临床》新版真题卷(附详细解析)
- 2023-2024学年福建省厦门市高二下学期期末质量检测数学试题(解析版)
- 中医药艾灸课件视频教学
- 《湖南省房屋建筑和市政工程消防质量控制技术标准》
- 2024年北京市东城区中考生物试题
- 成立生物安全委员会及工作制度管理办法和职责
评论
0/150
提交评论