Java实验3.docx

上传人:b****4 文档编号:4993098 上传时间:2022-12-12 格式:DOCX 页数:23 大小:51.68KB
下载 相关 举报
Java实验3.docx_第1页
第1页 / 共23页
Java实验3.docx_第2页
第2页 / 共23页
Java实验3.docx_第3页
第3页 / 共23页
Java实验3.docx_第4页
第4页 / 共23页
Java实验3.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

Java实验3.docx

《Java实验3.docx》由会员分享,可在线阅读,更多相关《Java实验3.docx(23页珍藏版)》请在冰豆网上搜索。

Java实验3.docx

Java实验3

实验3

继承与多态

一、实验目的:

1、学习与使用类的继承。

2、掌握关键字super的意义与用法。

3、学习掌握类的方法覆盖技术。

4、熟悉Object类,以及它提供给子类的方法equals、toString、clone。

5、学习掌握修饰符protected与final的用法。

6、学习掌握抽象类的概念与使用方法。

7、学习掌握多态性与动态绑定的概念,学习使用多态进行程序设计。

8、学习掌握接口的概念与定义接口的方法。

9、学习使用Cloneable接口与clone方法进行对象内容的复制。

10、理解浅复制与深复制的概念,掌握覆盖clone方法进行对象内容深复制的技术。

二、实验任务:

1、使用JavaSDK建立一个非图形化的标准Java程序学习与使用类的继承、掌握关键字super的意义与用法、掌握类的方法覆盖技术、熟悉Object类,以及它提供给子类的方法equals、toString、clone、学习掌握抽象类的概念与使用方法、学习掌握多态性与动态绑定的概念,学习使用多态进行程序设计。

程序要求:

(1)首先创建一个类家族,其中抽象类几何图形类GeometricObject为父类,圆类Circle与矩形类Rectangle为子类。

几何图形类GeometricObject中定义保护型字符串变量color,表示图形的颜色;该类要具备构造方法与两个抽象方法findArea与findPerimeter,抽象方法findArea求图形面积,抽象方法findPerimeter求图形周长。

(2)Circle类与Rectangle类就是GeometricObject类的子类,其中应实现父类的抽象方法。

(3)创建静态方法equalArea,用来比较图形的面积(不就是以上三个类的成员方法)。

方法名称如下:

staticbooleanequalArea(GeometricObjectobject1,GeometricObject

object2)

(4)创建静态方法displayGeometricObject,用来显示几何对象的信息(不就是以上三个类的成员方法)。

方法名称如下:

staticvoiddisplayGeometricObject(GeometricObjectobject)

(5)程序主方法中创建两个几何对象,一个圆与一个矩形,并用GeometricObject类的引用变量引用它们,调用equalArea比较两个对象的面积就是否相等,并调用displayGeometricObject方法显示对象信息。

2、使用JavaSDK建立一个非图形化的标准Java程序,进一步学习多态特性以及接口的概念与利用接口实现多态的方法。

程序要求如下:

(1)首先创建圆类Circle与圆柱体类Cylinder,其中Circle类就是父类,Cylinder类就是子类;

(2)创建接口Comparable,其中包含一个抽象方法compareTo,用来比较对象的大小。

抽象方法compareTo的形式如下:

publicintcompareTo(Objecto);

(3)创建类ComparableCircle,该类为Circle类的子类,并实现Comparable接口。

(4)创建类ComparableCylinder,该类为Cylinder类的子类,并实现Comparable接口。

(5)创建通用类Max,其中包含通用方法max,只要月一个类实现了Comparable接口,就可以使用max方法返回两个对象中较大的一个。

Max方法的方法名称为:

publicstaticComparablemax(Comparableo1,Comparableo2)

(6)程序的主方法中分别创建两个ComparableCircle类对象与两个ComparableCylinder类对象,并分别以它们为参数调用max方法,返回两个对象中面积较大的一个。

3、使用JavaSDK建立一个非图形化的标准Java程序,进一步深入学习多态特性以及利用Cloneable接口与clone方法实现对象内容的拷贝,并学习消除浅拷贝(浅复制)的方法。

程序要求如下:

(1)创建Circle类,表示圆;

(2)创建Name类,表示人名,其中包含三个String类型的数据成员:

firstName,middlName与lastName。

(3)创建CloneableCircle类,CloneableCircle类就是Circle类的子类,并实现了Cloneable接口。

要求CloneableCircle类中有一个Name类型的数据成员creator,代表圆对象的创建者姓名。

(4)在CloneableCircle类中实现clone方法,以实现两个CloneableCircle类对象内容的克隆。

要求实现对象内容深拷贝(深复制)。

(5)为了实现CloneableCircle类对象的深拷贝,Name类也应该实现Cloneable接口,并实现clone方法。

(6)程序的主方法中使用clone方法完成两个CloneableCircle类对象的深拷贝。

三、实验步骤:

1、使用Windows写字板编辑类GeometricObject,源程序如下:

publicabstractclassGeometricObject

{

protectedStringcolor;

protecteddoubleweight;

//Defaultconstruct

protectedGeometricObject()

{

color="white";

weight=1、0;

}

//Constructageometricobject

protectedGeometricObject(Stringcolor,doubleweight)

{

this、color=color;

this、weight=weight;

}

//Gettermethodforcolor

publicStringgetColor()

{

returncolor;

}

//Settermethodforcolor

publicvoidsetColor(Stringcolor)

{

this、color=color;

}

//Gettermethodforweight

publicdoublegetWeight()

{

returnweight;

}

//Settermethodforweight

publicvoidsetWeight(doubleweight)

{

this、weight=weight;

}

//Abstractmethod

publicabstractdoublefindArea();

//Abstractmethod

publicabstractdoublefindPerimeter();

}

2、使用Windows写字板编辑抽象类GeometricObject的派生类Circle,源程序如下:

publicclassCircleextendsGeometricObject

{

protecteddoubleradius;

//Defaultconstructor

publicCircle()

{

this(1、0,"white",1、0);

}

//Constructcirclewithspecifiedradius

publicCircle(doubleradius)

{

super("white",1、0);

this、radius=radius;

}

//Constructacirclewithspecifiedradius,weight,andcolor

publicCircle(doubleradius,Stringcolor,doubleweight)

{

super(color,weight);

this、radius=radius;

}

//Gettermethodforradius

publicdoublegetRadius()

{

returnradius;

}

//Settermethodforradius

publicvoidsetRadius(doubleradius)

{

this、radius=radius;

}

//ImplementthefindAreamethoddefinedinGeometricObject

publicdoublefindArea()

{

returnradius*radius*Math、PI;

}

//ImplementthefindPerimetermethoddefinedinGeometricObject

publicdoublefindPerimeter()

{

return2*radius*Math、PI;

}

//Overridetheequals()methoddefinedintheObjectclass

publicbooleanequals(Circlecircle)

{

returnthis、radius==circle、getRadius();

}

//OverridethetoString()methoddefinedintheObjectclass

publicStringtoString()

{

return"[Circle]radius="+radius;

}

}

3、使用Windows写字板编辑抽象类GeometricObject的派生类Rectangle,源程序如下:

publicclassRectangleextendsGeometricObject

{

protecteddoublewidth;

protecteddoubleheight;

//Defaultconstructor

publicRectangle()

{

this(1、0,1、0,"white",1、0);

}

//Constructarectanglewithspecifiedwidthandheight

publicRectangle(doublewidth,doubleheight)

{

this、width=width;

this、height=height;

}

//Constructarectanglewithspecifiedwidth,height,weight,andcolor

publicRectangle(doublewidth,doubleheight,

Stringcolor,doubleweight)

{

super(color,weight);

this、width=width;

this、height=height;

}

//Gettermethodforwidth

publicdoublegetWidth()

{

returnwidth;

}

//Settermethodforwidth

publicvoidsetWidth(doublewidth)

{

this、width=width;

}

//Gettermethodforheight

publicdoublegetHeight()

{

returnheight;

}

//Settermethodforheight

publicvoidsetHeight(doubleheight)

{

this、height=height;

}

//ImplementthefindAreamethodinGeometricObject

publicdoublefindArea()

{

returnwidth*height;

}

//ImplementthefindPerimetermethodinGeometricObject

publicdoublefindPerimeter()

{

return2*(width+height);

}

//Overridetheequals()methoddefinedintheObjectclass

publicbooleanequals(Rectanglerectangle)

{

return(width==rectangle、getWidth())&&

(height==rectangle、getHeight());

}

//OverridethetoString()methoddefinedintheObjectclass

publicStringtoString()

{

return"[Rectangle]width="+width+"andheight="+height;

}

}

4、使用Windows写字板编辑类TestPolymorphism,源代码如下:

publicclassTestPolymorphism

{

//Mainmethod

publicstaticvoidmain(String[]args)

{

//Declareandinitializetwogeometricobjects

GeometricObjectgeoObject1=newCircle(5);

GeometricObjectgeoObject2=newRectangle(5,3);

System、out、println("Thetwoobjectshavethesamearea?

"+

equalArea(geoObject1,geoObject2));

//Displaycircle

displayGeometricObject(geoObject1);

//Displayrectangle

displayGeometricObject(geoObject2);

}

//Amethodforcomparingtheareasoftwogeometricobjects

staticbooleanequalArea(GeometricObjectobject1,

GeometricObjectobject2)

{

returnobject1、findArea()==object2、findArea();

}

//Amethodfordisplayingageometricobject

staticvoiddisplayGeometricObject(GeometricObjectobject)

{

System、out、println();

System、out、println(object、toString());

System、out、println("Theareais"+object、findArea());

System、out、println("Theperimeteris"+object、findPerimeter());

}

}

5、把上面编辑的几个源程序保存成Java源程序文件(扩展名为java),程序文件名分别为GeometricObject、java、Circle、java、Rectangle、java、TestPolymorphism、java。

6、进入命令提示符状态,在源程序文件存放目录下对以上文件进行编译。

观察编译后源程序文件存放目录中的文件,熟悉Java程序的文件结构。

7、如果编译正确,则键入如下命令行,使用Java解释器运行源程序:

javaTestPolymorphism

程序运行结果如下:

8、使用Windows写字板编辑编辑类TestInterface源程序如下:

publicclassTestInterface

{

//Mainmethod

publicstaticvoidmain(String[]args)

{

//Createtwocomarablecircles

ComparableCirclecircle1=newComparableCircle(5);

ComparableCirclecircle2=newComparableCircle(4);

//Displaythemaxcircle

Comparablecircle=Max、max(circle1,circle2);

System、out、println("Themaxcircle'sradiusis"+

((Circle)circle)、getRadius());

System、out、println(circle);

//Createtwocomarablecylinders

ComparableCylindercylinder1=newComparableCylinder(5,2);

ComparableCylindercylinder2=newComparableCylinder(4,5);

//Displaythemaxcylinder

Comparablecylinder=Max、max(cylinder1,cylinder2);

System、out、println();

System、out、println("cylinder1'svolumeis"+

cylinder1、findVolume());

System、out、println("cylinder2'svolumeis"+

cylinder2、findVolume());

System、out、println("Themaxcylinder's\tradiusis"+

((Cylinder)cylinder)、getRadius()+"\n\t\t\tlengthis"+

((Cylinder)cylinder)、getLength()+"\n\t\t\tvolumeis"+

((Cylinder)cylinder)、findVolume());

System、out、println(cylinder);

}

}

//ComparableCircleisasubclassofCircle,whichimplementsthe

//Comparableinterface

classComparableCircleextendsCircleimplementsComparable

{

//ConstructaCompareCirclewithspecifiedradius

publicComparableCircle(doubler)

{

super(r);

}

//ImplementthecompareTomethoddefinedinComparable

publicintcompareTo(Objecto)

{

if(getRadius()>((Circle)o)、getRadius())

return1;

elseif(getRadius()<((Circle)o)、getRadius())

return-1;

else

return0;

}

}

//ComparableCylinderisasubclassofCylinder,whichimplementsthe

//CompareObjectinterface

classComparableCylinderextendsCylinderimplementsComparable

{

//ConstructaCompareCylinderwithradiusandlength

ComparableCylinder(doubler,doublel)

{

super(r,l);

}

//ImplementthecompareTomethoddefinedinComparableinterface

publicintcompareTo(Objecto)

{

if(findVolume()>((Cylinder)o)、findVolume())

return1;

elseif(findVolume()<((Cylinder)o)、findVolume())

return-1;

else

return0;

}

}

10、使用Windows写字板编辑编辑类Circle源程序如下:

publicclassCircle

{

protecteddoubleradius;

publicCircle()

{

radius=1、0;

}

//Constructcirclewithspecifiedradius

publicCircle(doubler)

{

radius=r;

}

//Gettermethodforradius

publicdoublegetRadius()

{

returnradius;

}

//Settermethodforradius

publicvoidsetRadius(doubleradius)

{

this、radius=radius;

}

//ImplementthefindAreamethoddefinedinGeometricObject

publicdoublefindArea()

{

returnradius*radius*Math、PI;

}

publicboole

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

当前位置:首页 > 求职职场 > 简历

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

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