收藏 分享(赏)

第八次实验报告.docx

上传人:林小宇 文档编号:509873 上传时间:2019-08-02 格式:DOCX 页数:5 大小:16.59KB
下载 相关 举报
第八次实验报告.docx_第1页
第1页 / 共5页
第八次实验报告.docx_第2页
第2页 / 共5页
第八次实验报告.docx_第3页
第3页 / 共5页
第八次实验报告.docx_第4页
第4页 / 共5页
第八次实验报告.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、第八次实验报告第八次实验实验 1:中国人、北京人和美国人 1.实验要求:编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有 4 个类:People、ChinaPeople、AmericanPeople 和 BeijingPeople 类。要求如下:People 类有权限是 protected 的 double 型成员变量height 和 weight,以及 publicvoid speakHello、public void averageHeight 和public void averageWeight 方法。ChinaPeople 类是 People 的子类,新增了 pub

2、lic void averageHeight 和 publicvoidaverageWeight 方法。AmericanPeople 类是 People 的子类,新增方法public void AmericanBoxing 。要求 AmericanPeople 重写父类的 public void speakHello、public void averageHeight 和 public void averageWeight 方法。BeijingPeople 类是 ChinaPeople 的子类,新增public void beijingOpera 方法。2.实验代码: /public clas

3、s People protected double weight,height; public void speakHello ( public void averageHeight height=173; ( public void averageWeight weight=70; ( /public class ChinaPeople extends People public void speakHello (您好 public void averageHeight height=; (中国人的平均身高:厘米 public void averageWeight weight=65; (中

4、国人的平均体重:千克 public void chinaGongfu (坐如钟,站如松,睡如弓 /public class AmericanPeople extends People public void speakHello ( public void averageHeight height=176; (厘米 public void averageWeight weight=75; ( public void americanBoxing (直拳,勾拳,组合拳 /public class BeijingPeople extends ChinaPeople public void aver

5、ageHeight height=; (北京人的平均身高:厘米 public void averageWeight weight=70; (北京人得平均体重:千克 public void beijingOpera (花脸、青衣、花旦和老生 /public class Example public static void main(String arg) ChinaPeople chinaPeople=new ChinaPeople ; AmericanPeople americanPeople=new AmericanPeople ; BeijingPeople beijingPeople=n

6、ew BeijingPeople ; ; ; ; ; ; ; ; ; ; ; ; ; ; 3.实验结果:4.实验分析:(1) 方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。这样,子类继承的方法才能被隐藏。(2) 子类在重写方法时,如果重写的方法是 static 方法,static 关键字必须保留;如果重写的方法是实例方法,重写时不可以用 static 修饰。(3) 如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的方法可以改变父类的具遗体行为。5.实验后的练习: People 类中的public void speakHello public

7、void averageHeight public void averageWeight三个方法的方法体中的语句是否可以省略。 答:可以省略,因为省略后结果没有变化实验 2:银行计算利息 1.实验要求:假设银行 bank 已经有了按整年 year 计算利息的一般方法,其中 year 只能取正整数。比如,按整年计算的方法:Double xxputerInternet Interest=year*saveMoney; Return interest; 建设银行 constructionBank 是 bankde 子类,准备隐藏继承的成员变量 year,并重写计算利息的方法,即自己声明一个 doub

8、le 型的 year 变量。要求 constructionbank和 bankofDalian 类是 bank 类的子类,constructionbank和 bankofdalian 都使用 super 调用隐藏的按整年计算利息的方法。2.实验代码:/public class Bank int savedMoney; int year; double interest; double interestRate=; public double xxputerInterest interest=year*interestRate*savedMoney; return interest; publi

9、c void setInterestRate( double rate) interestRate=rate; /public class ConstructionBank extends Bank double year; public double xxputerInterest =(int)year; double r=year-(int)year; int day=(int)(r*1000); double yearInterest= ; double dayInterest=day*savedMoney; interest=yearInterest+dayInterest; (元存在

10、建设银行%d 年零%d 天的利息:%f 元n return interest; /public class BankOfDalian extends Bank double year;public double xxputerInterest =(int)year; double r=year-(int)year; int day=(int)(r*1000);double yearInterest= ; double dayInterest=day*savedMoney; interest=yearInterest+dayInterest;(元存在大连银行%d 年零%d 天的利息:%f 元n return interest;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 思想汇报

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报