77范文网 - 专业文章范例文档资料分享平台

第八次实验报告

来源:网络收集 时间:2018-09-26 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

第八次实验

实验1:中国人、北京人和美国人 1.实验要求:

编写程序模拟中国人、美国人是人,北京人是中国人。除主类外,程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople 类。要求如下:

(1) People类有权限是protected的double型成员变量height和weight,以及public

void speakHello()、public void averageHeight()和public void averageWeight()方法。

(2) ChinaPeople类是People的子类,新增了public void averageHeight()和public

voidaverageWeight()方法。

(3) AmericanPeople类是People的子类,新增方法public void AmericanBoxing() 。

要求AmericanPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。

(4) BeijingPeople类是ChinaPeople的子类,新增public void beijingOpera()方

法。

2.实验代码: //People.java

public class People { protected double weight,height; public void speakHello() { System.out.println(\ } public void averageHeight() { height=173; System.out.println(\ } public void averageWeight() { weight=70; System.out.println(\ } }

//ChinaPeople.java

public class ChinaPeople extends People { public void speakHello() { System.out.println(\您好\ } public void averageHeight() { height=168.78; System.out.println(\中国人的平均身高:\厘米\ } public void averageWeight() {

weight=65; System.out.println(\中国人的平均体重:\千克\ } public void chinaGongfu() { System.out.println(\坐如钟,站如松,睡如弓\ } }

//AmericanPeople.java

public class AmericanPeople extends People { public void speakHello () { System.out.println(\ } public void averageHeight() { height=176; System.out.println(\厘米\ } public void averageWeight() { weight=75; System.out.println(\ } public void americanBoxing() { System.out.println(\直拳,勾拳,组合拳\ } }

//BeijingPeople.java

public class BeijingPeople extends ChinaPeople { public void averageHeight() { height=172.5; System.out.println(\北京人的平均身高:\厘米\ } public void averageWeight() { weight=70; System.out.println(\北京人得平均体重:\千克\ } public void beijingOpera() { System.out.println(\花脸、青衣、花旦和老生\ } }

//Example.java

public class Example { public static void main(String arg[]) {

ChinaPeople chinaPeople=new ChinaPeople(); AmericanPeople americanPeople=new AmericanPeople(); BeijingPeople beijingPeople=new BeijingPeople(); chinaPeople.speakHello(); americanPeople.speakHello(); beijingPeople.speakHello(); chinaPeople.averageHeight(); americanPeople.averageHeight(); beijingPeople.averageHeight(); chinaPeople.averageWeight(); americanPeople.averageWeight(); beijingPeople.averageWeight(); chinaPeople.chinaGongfu(); americanPeople.americanBoxing(); beijingPeople.beijingOpera(); beijingPeople.chinaGongfu(); } }

3.实验结果:

4.实验分析:

(1) 方法重写时要保证方法的名字、类型、参数的个数和类型同父类的某个方法完全想同。

这样,子类继承的方法才能被隐藏。

(2) 子类在重写方法时,如果重写的方法是static方法,static关键字必须保留;如果重写的

方法是实例方法,重写时不可以用static修饰。

(3) 如果子类可以继承父类的方法,子类就有权利重写这个方法,子类通过重写父类的方法

可以改变父类的具遗体行为。

5.实验后的练习: People类中的

public void speakHello() public void averageHeight() public void averageWeight()

三个方法的方法体中的语句是否可以省略。 答:可以省略,因为省略后结果没有变化

实验2:银行计算利息 1.实验要求:

假设银行bank已经有了按整年year计算利息的一般方法,其中year只能取正整数。比如,按整年计算的方法:

Double computerInternet(){ Interest=year*0.35*saveMoney; Return interest; }

建设银行constructionBank是bankde 子类,准备隐藏继承的成员变量year,并重写计算利息的方法,即自己声明一个double型的year变量。要求constructionbank和bankofDalian类是bank类的子类,constructionbank和bankofdalian都使用super调用隐藏的按整年计算利息的方法。

2.实验代码:

//Bank.java

public class Bank{ int savedMoney; int year; double interest; double interestRate=0.29; public double computerInterest(){ interest=year*interestRate*savedMoney; return interest; } public void setInterestRate( double rate){ interestRate=rate; } }

// ConstructionBank.java

public class ConstructionBank extends Bank{ double year; public double computerInterest(){ super.year=(int)year; double r=year-(int)year; int day=(int)(r*1000); double yearInterest=super.computerInterest(); double dayInterest=day*0.0001*savedMoney; interest=yearInterest+dayInterest; System.out.printf(\元存在建设银行%d年零%d天的利息:%f元\\n\ return interest; } }

// BankOfDalian.java

public class BankOfDalian extends Bank { double year;

public double computerInterest(){ super.year=(int)year; double r=year-(int)year; int day=(int)(r*1000);

double yearInterest=super.computerInterest(); double dayInterest=day*0.00012*savedMoney; interest=yearInterest+dayInterest;

System.out.printf(\元存在大连银行%d年零%d天的利息:%f元\\n\ return interest; } }

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库第八次实验报告在线全文阅读。

第八次实验报告.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/163215.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: