实验6继承与接口文档格式.docx

上传人:b****5 文档编号:16475560 上传时间:2022-11-24 格式:DOCX 页数:40 大小:1.09MB
下载 相关 举报
实验6继承与接口文档格式.docx_第1页
第1页 / 共40页
实验6继承与接口文档格式.docx_第2页
第2页 / 共40页
实验6继承与接口文档格式.docx_第3页
第3页 / 共40页
实验6继承与接口文档格式.docx_第4页
第4页 / 共40页
实验6继承与接口文档格式.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

实验6继承与接口文档格式.docx

《实验6继承与接口文档格式.docx》由会员分享,可在线阅读,更多相关《实验6继承与接口文档格式.docx(40页珍藏版)》请在冰豆网上搜索。

实验6继承与接口文档格式.docx

voidsetdata(Stringxm,intxh){

this.xm=xm;

this.xh=xh;

}

publicvoidprint(){

System.out.println(xm+"

"

+xh);

}

(2)编译源程序。

(二)创建将被继承的类

(1)程序功能:

通过EXP3_7类产生子类EXP3_8,其不仅具有父类的成员变量xm(姓名)、xh(学号),还定义了新成员变量xy(学院)、bj(bj)。

在程序中调用了父类的print方法,同时可以看出子类也具有该方法。

(2)程序源代码如下。

classEXP3_8extendsEXP3_7{

protectedStringxy;

protectedStringbj;

publicstaticvoidmain(String[]args){

EXP3_7p1=newEXP3_7();

p1.setdata("

李四"

12321);

p1.print();

EXP3_8s1=newEXP3_8();

s1.setdata("

张三"

12345);

s1.xy="

山西大学计算机学院"

;

s1.bj="

2008级计算机科学与技术"

s1.print();

System.out.println(s1.xm+"

+s1.xy+"

+s1.bj);

}

(1)编译并运行,结果如图3.7所示。

 

图3.7

(三)了解成员方法的覆盖方式

(1)编写覆盖Object类toString方法的程序文件EXP3_9.java,源代码如下。

classCircle{

privateintradius;

Circle(intradius){

this.radius=radius;

}//定义一个参数的构造方法

publicintgetRadius(){

returnradius;

publicvoidsetRadius(intradius){

this.radius=radius;

publicdoublearea(){

returnMath.PI*radius*radius;

publicStringtoString(){

return"

Radius:

"

+getRadius()+"

area:

+area();

}

publicclassEXP3_9{

publicstaticvoidmain(Stringargs[]){

Circlec=newCircle(10);

System.out.println("

\n"

+c.toString());

(2)编译并运行,结果如图3.8所示。

图3.8

(3)试着以Point类为例,尝试为Object类的clone()和equals()方法进行覆盖,Point类包含私有成员x,y,构造方法1(包含两个参数a,b),构造方法2(参数为Pointp),clone方法,equals方法,toString方法。

用TestPoint类进行测试。

classPointextendsObject{

privateintx;

privateinty;

Point(inta,intb){

this.x=a;

this.y=b;

Point(Pointp){

this.x=p.x;

this.y=p.y;

publicintgetX(){

returnx;

publicvoidsetX(intx){

this.x=x;

publicintgetY(){

returny;

publicvoidsetY(inty){

this.y=y;

protectedStringclone(){

returnthis.x+"

"

+this.y;

publicbooleanequals(Pointp){

if(this.x==p.x&

&

this.y==p.y)

returntrue;

elsereturnfalse;

publicStringtoSting(){

x是:

+this.x+"

y是:

publicclassTestPoint{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Pointp1=newPoint(1,2);

Pointp2=newPoint(1,2);

Pointp3=newPoint(3,4);

P1:

+p1.getX()+"

+p1.getY());

System.out.println(p1.equals(p2));

System.out.println(p1.equals(p3));

System.out.println(p3.clone());

System.out.println(p2.toSting());

System.out.println(p3.toSting());

(4)编译并运行结果如图3.9

图3.9

(四)this、super和super()的使用

程序功能:

说明this、super和super()的用法。

程序首先定义Point(点)类,然后创建点的子类Line(线)。

最后通过LX3_10类输出线段的长度。

程序中通过super(a,b)调用父类Point的构造方法为父类的x和y赋值。

在子类Line的setLine方法中,因为参数名和成员变量名相同,为给成员变量赋值,使用this引用,告诉编译器是为当前类的成员变量赋值。

在length和toString方法中使用父类成员变量时,使用super引用,告诉编译器使用的是父类的成员变量。

classPoint1{

protectedintx,y;

Point1(inta,intb){

setPoint(a,b);

publicvoidsetPoint(inta,intb){

x=a;

y=b;

classLineextendsPoint1{

Line(inta,intb){

super(a,b);

setLine(a,b);

publicvoidsetLine(intx,inty){

this.x=x+x;

this.y=y+y;

publicdoublelength(){

intx1=super.x,y1=super.y,x2=this.x,y2=this.y;

returnMath.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));

直线端点:

["

+super.x+"

+super.y+"

]["

+

x+"

+y+"

]直线长度:

+this.length();

publicclassEXP3_10{

Lineline=newLine(50,50);

+line.toString());

(3)编译并运行,结果如图3.10。

图3.10

五)1、定义父类People,分别定义People类的子类ChinaPeople,AmericanPeople和BeijingPeople并分别重写父类中的各个方法。

最后在主方法中分别创建各子类的对象并调用各自的方法打印输出信息。

该程序的模板代码如下:

请将其补充完整并调试运行。

classPeople

{

protecteddoubleweight,height;

publicvoidspeakHello()

{

System.out.println("

yayawawa"

);

}

publicvoidaverageHeight()

{

height=173;

averageheight:

+height);

publicvoidaverageWeight()

weight=70;

averageweight:

+weight);

classChinaPeopleextendsPeople

{

你好,吃了吗?

}

height=165;

“中国人的平均身高:

+height+"

厘米"

weight=65;

中国人的平均体重:

+weight+"

公斤"

publicvoidchinaGongfu()

坐如钟,站如松,睡如弓"

classAmericanPeopleextendsPeople

Howdoyoudo"

height=175;

weight=67;

publicvoidamericanBoxing()

直拳"

+"

、"

钩拳"

classBeijingPeopleextendsChinaPeople

您好"

height=168;

weight=60;

publicvoidbeijingOpera()

System.out.println("

BeijingOpera"

publicclassExample

publicstaticvoidmain(Stringargs[])

ChinaPeoplechinaPeople=newChinaPeople();

AmericanPeopleamericanPeople=newAmericanPeople();

BeijingPeoplebeijingPeople=newBeijingPeople();

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.11所示:

图3.11

2、读懂下面模板代码,按要求补充程序并调试运行。

掌握抽象类的定义及其实现方法,学习上转型对象的运用方法。

abstractclassEmployee

publicabstractdoubleearnings();

classYearWorkerextendsEmployee

publicdoubleearnings(){

return60000;

classMonthWorkerextendsEmployee

return5000;

classWeekWorkerextendsEmployee

return1200;

classCompany

Employee[]employee;

doublesalaries=0;

Company(Employee[]employee)

this.employee=employee;

publicdoublesalariesPay()

salaries=0;

for(inti=0;

i<

employee.length;

i++){

salaries+=employee[i].earnings();

returnsalaries;

publicclassHardwork{

Employee[]employee=newEmployee[20];

i++)

if(i%3==0)

employee[i]=newWeekWorker();

elseif(i%3==1)

employee[i]=newMonthWorker();

elseif(i%3==2)

employee[i]=newYearWorker();

Companycompany=newCompany(employee);

公司年工资总额:

+company.salariesPay());

编译并运行程序结果如图3.12

图3.12

3、读懂下面模板代码,按要求补充程序并调试运行。

掌握接口的定义及其实现方法,学习接口回调的运用方法。

interfaceComputerWeight{

publicdoublecomputeWeight();

classTelevisionimplementsComputerWeight{

publicdoublecomputeWeight(){

return30.5;

classComputerimplementsComputerWeight{

return25;

}

classWashMachineimplementsComputerWeight{

return45;

classCar{

ComputerWeight[]goods;

doubletotalWeights=0;

Car(ComputerWeight[]goods){

this.goods=goods;

publicdoublegetTotalWeights(){

totalWeights=0;

goods.length;

totalWeights+=goods[i].computeWeight();

returntotalWeights;

publicclassRoad{

ComputerWeight[]goodsOne=newComputerWeight[50],

goodsTwo=newComputerWeight[22];

goodsOne.length;

goodsOne[i]=newTelevision();

goodsOne[i]=newComputer();

goodsOne[i]=newWashMachine();

goodsTwo.length;

goodsTwo[i]=newTelevision();

goodsTwo[i]=newComputer();

goodsTwo[i]=newWashMachine();

Car大货车=newCar(goodsOne);

大货车装载的货物重量:

+大货车.getTotalWeights());

Car小货车=newCar(goodsTwo);

小货车装载的货物重量:

+小货车.getTotalWeights());

编译并运行结果如图3.13所示

图3.13

(六)接口的实现与运用

实验任务:

本实验的任务是设计和实现一个Soundable接口,该接口具有发声功能,同时还能够调节声音大小。

Soundable接口的这些功能将会由3种声音设备来具体实现,它们分别是收音机Radio、随身昕Walkman和手机Mobilephone。

最后还要设计一个应用程序类来使用这些实现了Soundable接口的声音设备类。

程序运行时,先询问用户想听哪种设备,然后程序就会按照该设备的工作方式来发出声音。

实验步骤:

(1)仔细阅读程序清单1-9,并完成其中的代码1~代码3。

程序清单1-9InterfaceTest.java

importjava.util.Scanner;

interfaceSoundable{

publicvoidincreaseVolume();

publicvoiddecreaseVolume();

publicvoidstopSound();

publicvoidplaySound();

classRadioimplementsSoundable{

publicvoidincreaseVolume(){

增大收音机音量"

publicvoiddecreaseVolume(){

减小收音机音量"

publicvoidstopSound(){

关闭收音机"

publicvoidplaySound(){

收音机播放广播"

classWalkmanimplementsSoundable{

增大随声听音量"

System.out.pri

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 设计艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1