JAVA例81815及实验3238Word文档下载推荐.docx

上传人:b****8 文档编号:22433386 上传时间:2023-02-04 格式:DOCX 页数:38 大小:106.65KB
下载 相关 举报
JAVA例81815及实验3238Word文档下载推荐.docx_第1页
第1页 / 共38页
JAVA例81815及实验3238Word文档下载推荐.docx_第2页
第2页 / 共38页
JAVA例81815及实验3238Word文档下载推荐.docx_第3页
第3页 / 共38页
JAVA例81815及实验3238Word文档下载推荐.docx_第4页
第4页 / 共38页
JAVA例81815及实验3238Word文档下载推荐.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

JAVA例81815及实验3238Word文档下载推荐.docx

《JAVA例81815及实验3238Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《JAVA例81815及实验3238Word文档下载推荐.docx(38页珍藏版)》请在冰豆网上搜索。

JAVA例81815及实验3238Word文档下载推荐.docx

+department+"

的学生"

publicclassApp8_1

publicstaticvoidmain(String[]args)

Studentstu=newStudent();

stu.setNameAge("

张小三"

21);

stu.show();

stu.setDepartment("

计算机系"

 

例8.2

App8_2.java

调用了Person类的无参构造方法"

publicPerson(Stringname,intage)

调用了Person类的有参构造方法"

调用了学生类的无参构造方法Student()"

publicStudent(Stringname,intage,Stringdep)

super(name,age);

调用了学生类的有参构造方法Student(Stringname,intage,Stringdep)"

publicclassApp8_2

Studentstu1=newStudent();

Studentstu2=newStudent("

李小四"

23,"

信息系"

stu1.show();

stu2.show();

例8.3

App8_3.java

protectedintage;

publicPerson(){} 

//定义Person类的“不做事”的无参构造方法

protectedvoidshow()

intage=20;

publicStudent(Stringxm,Stringdep)

name=xm;

super.age=25;

之类Studetn中的成员变量age="

super.show();

系别:

+department);

publicclassApp8_3

Studentstu=newStudent("

"

例8.4

App8_4.java

protectedStringname;

publicclassApp8_4

王永涛"

24,"

电子"

例8.5

App8_5.java

publicvoidsubshow()

我在之类中"

publicclassApp8_5

Personper=newStudent("

per.show();

//per.subshow();

例8.6

/*

父类中被声明为final的成员在子类中可被继承,但不能被覆盖。

filename:

App8_6.java

父类中的final方法不允许被覆盖;

*/

classAAA

staticfinaldoublePI=3.14;

//声明静态常量

publicfinalvoidshow()//声明最终方法,无法被子类覆盖

pi="

+PI);

classBBBextendsAAA

privateintnum=100;

publicvoidshow()//错误,不可以覆盖父类中的最终方法

num="

+num);

publicclassApp8_6

BBBex=newBBB();

ex.show();

例8.7

使用“==”和equals()比较对象的异同。

classA{

inta=1;

publicclassApp8_7{

publicstaticvoidmain(String[]args){

Aobj1=newA();

Aobj2=newA();

Strings1,s2,s3="

abc"

s4="

;

//s3、s4指向同一字符串“abc”

s1=newString("

s2=newString("

s1.equals(s2)是"

+(s1.equals(s2)));

s1==s3是"

+(s1==s3));

s1.equals(s3)是"

+(s1.equals(s3)));

s3==s4是"

+(s3==s4));

s2.equals(s3)是"

+(s2.equals(s3)));

s1==s2是"

+(s1==s2));

obj1==obj2是"

+(obj1==obj2));

obj1.equals(obj2)是"

+(obj1.equals(obj2)));

obj1=obj2;

obj1=obj2后obj1==obj2是"

obj1=obj2后obj1.equals(obj2)是"

例8.8

filename:

App8_8.java;

利用getClass()方法返回运行该方法的对象所属的类

publicPerson(Stringxm) 

//定义Person类的构造方法;

name=xm;

publicclassApp8_8 

//主类

Personper=newPerson("

张三"

Classobj=per.getClass();

//利用对象per调用getClass()方法,返回per所属的类;

对象per所属的类为:

+obj);

例8.9运算符instanceof及getName()、getSuperclass()方法的使用。

//filenamePerson.java

publicclassPerson//定义person类

staticintcount=0;

protectedStringname;

protectedintage;

publicPerson(Stringn1,inta1)

{

name=n1;

age=a1;

this.count++;

//调用父态的静态变量

}

publicStringtoString()

returnthis.name+"

+this.age;

publicvoiddisplay()

System.out.print("

本类名="

+this.getClass().getName()+"

System.out.println("

父类名="

+this.getClass().getSuperclass().getName());

Person.count="

+this.count+"

"

Student.count="

+Student.count+"

Objectobj=this;

if(objinstanceofStudent)//判断对象属于哪个类

System.out.println(obj.toString()+"

是Student类对象。

elseif(objinstanceofPerson)

是Person类对象。

classStudentextendsPerson//子类Student继承父类Person,且是主类但不是public类

//隐藏了父类的count

protectedStringdept;

protectedStudent(Stringn1,inta1,Stringd1)

super(n1,a1);

//调用父类的构造方法

dept=d1;

//调用子类的静态变量

publicStringtoString()//覆盖父类的同名方法

returnsuper.toString()+"

+dept;

//调用父类的同名方法

super.display();

super.count="

+super.count);

this.count="

+this.count);

publicstaticvoidmain(String[]args)

Personper=newPerson("

王涛"

23);

per.display();

Studentstu=newStudent("

22,"

stu.display();

程序运行结果及其分析:

例8.10抽象类的应用举例,定义一个形状抽象类Shape,以该抽象类为父类派生出圆形子类Circle和矩形子类Rectangle。

//filenameApp8_10.java

abstractclassShape//定义形状抽象类Shape

publicShape(Stringxm)//抽象类中的一般方法,本方法是构造方法

name=xm;

名称:

+name);

abstractpublicdoublegetArea();

//声明抽象方法

abstractpublicdoublegetlength();

classCircleextendsShape//定义继承自Shape的圆形子类Circle

privatefinaldoublePI=3.14;

privatedoubleradius;

publicCircle(StringshapeName,doubler)//构造方法

super(shapeName);

radius=r;

publicdoublegetArea()//实现抽象类中的getArea()方法

returnPI*radius*radius;

publicdoublegetlength()//实现抽象类中的getlength()方法

return2*PI*radius;

classRectangleextendsShape

privatedoublewidth;

privatedoubleheight;

publicRectangle(StringshapeName,doublewidth,doubleheight)

this.width=width;

this.height=height;

publicdoublegetArea()

returnwidth*height;

publicdoublegetlength()

return2*(width+height);

publicclassApp8_10

Shaperect=newRectangle("

长方形"

6.5,10.3);

面积="

+rect.getArea()+"

;

周长="

+rect.getlength()+"

\n"

Shapecircle=newCircle("

圆"

10.2);

+circle.getArea()+"

+circle.getlength());

例8.11利用形状接口IShape建造类。

//filenameApp8_11.java

interfaceIShape

finaldoublePI=3.14;

abstractdoublegetArea();

abstractdoublegetlength();

classCircleimplementsIShape

doubleradius;

publicCircle(doubler)

classRectangleimplementsIShape

publicRectangle(doublewidth,doubleheight)

publicclassApp8_11

IShapecircle=newCircle(5.0);

圆的面积="

周长="

+circle.getlength()+"

Rectanglerect=newRectangle(6.5,10.8);

矩形面积="

+rect.getlength());

例8.12接口的继承。

//filenameCylinder.java

interfaceFace1

abstractdoublearea();

interfaceFace2

abstractvoidsetColor(Stringc);

interfaceFace3extendsFace1,Face2

abstractvoidvolume();

publicclassCylinderimplementsFace3

privateintheight;

protectedStringColor;

publicCylinder(doubler,inth)

height=h;

publicdoublearea()

publicvoidsetColor(Stringc)

Color=c;

颜色:

+Color);

publicvoidvolume()

圆柱体体积:

+area()*height);

Cylindervolu=newCylinder(3.0,2);

volu.setColor("

红色"

volu.volume();

例8.13利用接口实现类的多重继承。

ab

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

当前位置:首页 > 工作范文 > 其它

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

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