实验五 Java的类及多态性.docx

上传人:b****5 文档编号:6099686 上传时间:2023-01-03 格式:DOCX 页数:23 大小:180.21KB
下载 相关 举报
实验五 Java的类及多态性.docx_第1页
第1页 / 共23页
实验五 Java的类及多态性.docx_第2页
第2页 / 共23页
实验五 Java的类及多态性.docx_第3页
第3页 / 共23页
实验五 Java的类及多态性.docx_第4页
第4页 / 共23页
实验五 Java的类及多态性.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

实验五 Java的类及多态性.docx

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

实验五 Java的类及多态性.docx

实验五Java的类及多态性

实验五Java的类及多态性

一、实验目的:

1.掌握Java类及多态性的含义。

2.掌握instanceof的使用方法。

3.掌握应用变量的转换。

二、实验要求:

编写一个能体现多态性的Java应用程序。

三、实验内容:

(1)类

1、常量的声明:

声明一个常量PI,编译它,查看有何错误并修改。

错误:

classPoint

{intx,y;

staticintz;

finaldoublePI=3.1415926;[修改:

赋值3.1415926]

Point(inta,intb)

{x=a;

y=b;

}

Point()

{this(1,1);

}

staticvoidoutput()

{System.out.println("output()called");

System.out.println(z);

}

voidoutput(inta,inty)

{output();

z=5;

this.x=x;

this.y=y;

}

publicstaticvoidmain(String[]args)

{Pointpt1=newPoint();

Pointpt2=newPoint();

pt1.z=5;

pt2.z=6;

System.out.println(pt1.z);

}

}

2、声明一个静态的常量:

修改上题的前半部分,如下。

编译,查看有何错误,并修改

错误:

classPoint

{intx,y;

staticintz;

finaldoublePI;[修改:

删除static。

]

Point(inta,intb)

{PI=3.1415926;

x=a;

y=b;

}

Point()

{this(1,1);

}

staticvoidoutput()

{System.out.println("output()called");

System.out.println(z);

}

voidoutput(inta,inty)

{output();

z=5;

this.x=x;

this.y=y;

}

publicstaticvoidmain(String[]args)

{Pointpt1=newPoint();

Pointpt2=newPoint();

pt1.z=5;

pt2.z=6;

System.out.println(pt1.z);

}

}

3、继承:

新建一个Animal.java文件,并编译它。

查看源文件的当前目录下生成了几个类文件,并选择一个执行它,输出结果。

classAnimal

{intheight,weight;

voideat()

{System.out.println("animaleat");

}

voidsleep()

{System.out.println("animalsleep");

}

voidbreathe()

{System.out.println("animalbreathe");

}

}

classFishextendsAnimal

{

}

classIntegration

{

publicstaticvoidmain(String[]args)

{Animalan=newAnimal();

Fishfh=newFish();

an.breathe();

fh.height=30;

fh.breathe();

}

}

4、修改上题中的classFishextendsAnimal类,如下,并执行它,查看结果。

classFishextendsAnimal

{voidbreathe()

{System.out.println("animalbubble");

}

}

5、再次修改classFishextendsAnimal类和Integration类,如下,并执行,查看结果。

并思考课件中对于super的说明。

classAnimal

{intheight,weight;

voideat()

{System.out.println("animaleat");

}

voidsleep()

{System.out.println("animalsleep");

}

voidbreathe()

{System.out.println("animalbreathe");

}

}

classFishextendsAnimal

{intheight;

voidbreathe()

{super.breathe();

super.height=40;

System.out.println("fishbubble");

}

}

classIntegration

{

publicstaticvoidmain(String[]args)

{//Animalan=newAnimal();

Fishfh=newFish();

//an.breathe();

fh.height=30;

fh.breathe();

}

}

Super用法:

☐提供了对父类的访问。

☐可以使用super访问父类被子类隐藏的变量或覆盖的方法。

6、分别在Animal类和Fish类中,添加构造方法如下:

并将Integration类,修改如下

classAnimal

{intheight,weight;

voideat()

{System.out.println("animaleat");

}

voidsleep()

{System.out.println("animalsleep");

}

voidbreathe()

{System.out.println("animalbreathe");

}

Animal()

{System.out.println("animalconstruct");

}

}

classFishextendsAnimal

{intheight;

voidbreathe()

{super.breathe();

super.height=40;

System.out.println("fishbubble");

}

Fish()

{System.out.println("fishconstruct");

}

}

classIntegration

{

publicstaticvoidmain(String[]args)

{//Animalan=newAnimal();

Fishfh=newFish();

}

}

查看执行结果,并思考为什么

思考:

当实类化对象时,会默认执行里边的构造方法。

7、执行下边程序,并修改错误

classAnimal

{intheight,weight;

Animal(intheight,intweight)

{System.out.println("animalconstruct");

}

voideat()

{System.out.println("animaleat");

}

voidsleep()

{System.out.println("animalsleep");

}

voidbreathe()

{System.out.println("animalbreathe");

}

}

classFishextendsAnimal

{intheight;

Fish()

{super();

System.out.println("fishconstruct");

}

voidbreathe()

{//super.breathe();

//super.height=40;

System.out.println("fishbubble");

}

}

classIntegration

{

publicstaticvoidmain(String[]args)

{//Animalan=newAnimal();

Fishfh=newFish();

}

}

修改:

给super赋值。

(2)多态性

1、注意成员变量和成员方法的调用,查看输出结果

classAnimal

{intheight=1,weight=1;

Animal()

{

}

voideat()

{System.out.println("animaleat");

}

voidsleep()

{System.out.println("animalsleep");

}

voidbreathe()

{System.out.println("animalbreathe");

}

}

classFishextendsAnimal

{intheight=10;

Fish()

{

}

voidbreathe()

{

System.out.println("fishbreathe");

}

voidswim()

{

System.out.println("fishswim");

}

}

classIntegration

{

staticvoidfn(Animalan)

{

an.breathe();

}

publicstaticvoidmain(String[]args)

{Animalan=newAnimal();

System.out.println(an.height);

an.eat();

an.breathe();

Fishfh=newFish();

System.out.println(fh.height);

fh.breathe();

fh.swim();

Animalx=newFish();

System.out.println(x.height);

x.eat();

x.breathe();

}

}

2、在main方法中,用x调用swim方法,添加代码如下:

x.swim();

编译后,发现有何错误?

修改:

引用变量只能调用声明该变量时所用类里包含的方法。

3、instanceof方法的使用

在上题main方法中添加如下代码,并写出结果

classAnimal

{intheight=1,weight=1;

Animal()

{

}

voideat()

{System.out.println("animaleat");

}

voidsleep()

{System.out.println("animalsleep");

}

voidbreathe()

{System.out.println("animalbreathe");

}

}

classFishextendsAnimal

{intheight=10;

Fish()

{

}

voidbreathe()

{

System.out.println("fishbreathe");

}

voidswim()

{

System.out.println("fishswim");

}

}

classIntegration

{

staticvoidfn(Animalan)

{

an.breathe();

}

publicstaticvoidmain(String[]args)

{Animalan=newAnimal();

System.out.println(an.height);

an.eat();

an.breathe();

Fishfh=newFish();

System.out.println(fh.height);

fh.breathe();

fh.swim();

Animalx=newFish();

System.out.println(x.height);

x.eat();

x.breathe();

Fishfh2=(Fish)x;

fh2.swim();

if(xinstanceofFish)

{System.out.println("xisfish'sinstance");

}

else

{System.out.println("xisn'tfish'sinstance");

}

if(fhinstanceofAnimal)

{System.out.println("fhisanimal'sinstance");

}

else

{System.out.println("fhisn'tanimal'sinstance");

}

}

}

4、引用变量之间的强制类型转换

阅读程序,并编译,查看有何错误

publicclassTestConversion

{

publicstaticvoidmain(String[]args)

{

doubled=13.4;

longl=(long)d;

System.out.println(l);

intin=5;

//下面代码编译时出错:

试图把一个数值型变量转换为boolean型,

//编译时候会提示:

不可转换的类型

//booleanb=(boolean)in;

Objectobj="Hello";

//obj变量的编译类型为Object,是String类型的父类,可以强制类型转换

//而且obj变量实际上类型也是String类型,所以运行时也可通过

StringobjStr=(String)obj;

System.out.println(objStr);

//定义一个objPri变量,编译类型为Object,实际类型为Integer

ObjectobjPri=newInteger(5);

//objPri变量的编译类型为Object,是String类型的父类,可以强制类型转换

//而objPri变量实际上类型是Integer类型,所以下面代码运行时引发ClassCastException异常

Stringstr=(String)objPri;

}

}

Integer不能强制转化成String,这里应该用到toString或者String.valueOf()。

5、复习递归的方法

已知有一个数列:

f(20)=1,f(21)=4,f(n+2)=2*f(n+1)+f(n);其中,n是大于零的整数,求f(10)的值。

classRecursionDemo

{publicstaticintDemo(intn)

{if(n==20)

{return1;

}

elseif(n==21)

{return4;

}

else

{returnDemo(n+2)-2*Demo(n+1);

}

}

publicstaticvoidmain(Stringargs[])

{System.out.println(Demo(10));

}

}

已知有一个数列:

f(0)=1,f

(1)=4,f(n+2)=2*f(n+1)+f(n);其中,n是大于零的整数,求f(10)的值。

classRecursionDemo

{publicstaticintDemo(intn)

{if(n==0)

{return1;

}

elseif(n==1)

{return4;

}

else

{return2*Demo(n-1)+Demo(n-2);

}

}

publicstaticvoidmain(Stringargs[])

{System.out.println(Demo(10));

}

}

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

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

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

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