实验二 继承与接口实验_第1页
实验二 继承与接口实验_第2页
实验二 继承与接口实验_第3页
实验二 继承与接口实验_第4页
实验二 继承与接口实验_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验二继承与接口一、实验目的1.掌握类的继承机制。2.熟悉类中成员变量和方法的访问控制。3.掌握接口与包的使用,熟悉方法的多态性。二、实验内容1.定义父类及子类,在子类中重写父类的方法2.练习接口与包的使用三、.实验步骤与要求第1题继承编写一个Java应用程序,除了主类外,该程序中还有4个类:People,ChinaPeople,AmericanPeople和BeijingPeople类。此四个类的继承关系如下图所示:要求ChinaPeopl,eAmerican类均重写其父类People类的speakHello,averageHeight,averageWe方法ht,BeijingPeop类重

2、写其父类ChinaPeopl类的speakHello,averageHeight,averageWe法。htPeople类变量方法protecteddoubleheightpublicvoidspeakHello()protecteddoubleweightpublicvoidaverageHeight。publicvoidaverageWeight()ChinaPeople类方法:publicvoidchinaGongfu()AmericanPeople类方法:PublicvoidamericanBoxing()BeijingPeople类方法:PublicvoidbeijingOpera(

3、)源代码:packagepeople;classpeopleprotecteddoubleheight;protecteddoubleweight;publicvoidspeakHello()/问候语的函数System.out.println(hello);publicvoidaverageHeight()/人们的平均身高height=170;System.out.println(+height);publicvoidaverageWeight()/人们的平均体重weight=120;System.out.println(+weight);classChinapeopleextendspeop

4、lepublicvoidspeakHello()System.out.println(你好);publicvoidaverageHeight()height=172;System.out.println(+height);publicvoidaverageWeight()weight=115;System.out.println(+weight);publicvoidchinaGongfu()/中国功夫的方法System.out.println(中国功夫);classAmericanpeopleextendspeoplepublicvoidspeakHello()System.out.prin

5、tln(hello);publicvoidaverageHeight()height=180;System.out.println(+height);publicvoidaverageWeight()weight=150;System.out.println(+weight);publicvoidamericanBoxing()/美国拳击的方法System.out.println(americanBoxing);classBeijingpeopleextendsChinapeoplepublicvoidspeakHello()System.out.println(北京欢迎你);publicvo

6、idaverageHeight()height=168;System.out.println(+height);publicvoidaverageWeight()weight=125;System.out.println(+weight);classExamplepublicstaticvoidmain(Stringargs)peoplep=newpeople();Chinapeoplec=newChinapeople();Americanpeoplea=newAmericanpeople();Beijingpeopleb=newBeijingpeople();p.averageHeight(

7、);p.averageWeight();p.speakHello();c.averageHeight();c.averageWeight();c.chinaGongfu();c.speakHello();a.averageHeight();a.averageWeight();a.americanBoxing();a.speakHello();b.averageHeight();b.averageWeight();b.speakHello();结果截图:奥ProblemsJavaidoc层DechratiotnExample(3)JavaApplication170.012,0hello172.

8、0115.6中国正夫:180.0150.0americanBoxinghello168.0125.0北京一双阻弟第2题上转型对象要求有一个abstract类,类名为Employee。Employee的子类有YearWorker,MonthWorker和WeekWorker。YearWorker按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:publicabstractearnings();子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。有一个Company类,该类用employee数组作为

9、成员,employee数组的单元可以是YearWorker对象的上转型对象、MonthWorker对象的上转型对象或WeekWorke对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。源代码:packagepeople;abstractclassEmployeeintsalary;publicabstractvoidearnings();classYearWorkerextendsEmployeepublicvoidearnings()salary=200;System.out.println(年薪:+salary*365);classMonthWorkerextendsE

10、mployeepublicvoidearnings()salary=100;System.out.println(月薪:+salary*30+一年薪水:+salary*30*12);classWeekWorkerextendsEmployeepublicvoidearnings()salary=80;System.out.println(周薪:+salary*7+一年薪水:+salary*365);publicclassCompanypublicstaticvoidmain(Stringargs)Employeee=newEmployee3;e0=newYearWorker();e0.earn

11、ings();e1=newMonthWorker();e1.earnings();e2=newWeekWorker();e2.earnings();结果截图:盟ProblemsJavadoc圜CompanyJava.年译:73&00AM:39&0一年承水;36&00,flF=56&一年隼水;2920第3题接口回调要求有一个ComputeTotalSales接口,该接口中有一个方法:publicdouble321521058丫丫。21(),有三个实现该接口的类:Television,Computer和Mobile。这三个类通过实现接口ComputeTotalSales,给出自己的年销售额。有一个

12、Shop类,该类用ComputeTotalSales数组作为成员,ComputeTotalSales数组的单元可以存放Television对象的引用、Computer对象的引用或Mobile对象的引用。程序能输出Shop对象的年销售额。源代码:packagepeople;interfaceComputeTotalSalespublicdoubletotalSalesByYear();classTelevisionimplementsComputeTotalSalespublicdoubletotalSalesByYear()doublesale=100;returnsale*365;class

13、computerimplementsComputeTotalSalespublicdoubletotalSalesByYear()doublesale=200;returnsale*365;classMobileimplementsComputeTotalSalespublicdoubletotalSalesByYear()doublesale=220;returnsale*365;publicclassshoppublicstaticvoidmain(Stringargs)ComputeTotalSalesc;c=newComputeTotalSales3;c0=newTelevision();System.out.println(电视机的年销售量:+c0.totalSalesByYear();c1=newcomputer();System.out.println(电脑的年销售量:+c1.totalSalesByYear();c2=newMobile();System.out.println(手机的年销售量:+c2.totalSalesByYear();结果截图:京Ecte

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论