JAVA例81815及实验3238.docx
- 文档编号:9297313
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:38
- 大小:106.65KB
JAVA例81815及实验3238.docx
《JAVA例81815及实验3238.docx》由会员分享,可在线阅读,更多相关《JAVA例81815及实验3238.docx(38页珍藏版)》请在冰豆网上搜索。
JAVA例81815及实验3238
14网2第12组第七周上机报告
例8.1
//filename:
App8_1.java
classPerson
{
privateStringname;
privateintage;
publicPerson()
{
System.out.println("调用了个人类的构造方法Person()");
}
publicvoidsetNameAge(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicvoidshow()
{
System.out.println("姓名:
"+name+" 年龄:
"+age);
}
}
classStudentextendsPerson
{
privateStringdepartment;
publicStudent()
{
System.out.println("调用了学生类的构造方法Student()");
}
publicvoidsetDepartment(Stringdep)
{
department=dep;
System.out.println("我是"+department+"的学生");
}
}
publicclassApp8_1
{
publicstaticvoidmain(String[]args)
{
Studentstu=newStudent();
stu.setNameAge("张小三",21);
stu.show();
stu.setDepartment("计算机系");
}
}
例8.2
//filename:
App8_2.java
classPerson
{
privateStringname;
privateintage;
publicPerson()
{
System.out.println("调用了Person类的无参构造方法");
}
publicPerson(Stringname,intage)
{
System.out.println("调用了Person类的有参构造方法");
this.name=name;
this.age=age;
}
publicvoidshow()
{
System.out.println("姓名:
"+name+" 年龄:
"+age);
}
}
classStudentextendsPerson
{
privateStringdepartment;
publicStudent()
{
System.out.println("调用了学生类的无参构造方法Student()");
}
publicStudent(Stringname,intage,Stringdep)
{
super(name,age);
department=dep;
System.out.println("我是"+department+"的学生");
System.out.println("调用了学生类的有参构造方法Student(Stringname,intage,Stringdep)");
}
}
publicclassApp8_2
{
publicstaticvoidmain(String[]args)
{
Studentstu1=newStudent();
Studentstu2=newStudent("李小四",23,"信息系");
stu1.show();
stu2.show();
}
}
例8.3
//filename:
App8_3.java
classPerson
{
protectedintage;
publicPerson(){} //定义Person类的“不做事”的无参构造方法
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
protectedvoidshow()
{
System.out.println("姓名:
"+name+" 年龄:
"+age);
}
}
classStudentextendsPerson
{
privateStringdepartment;
intage=20;
publicStudent(Stringxm,Stringdep)
{
name=xm;
department=dep;
super.age=25;
System.out.println("之类Studetn中的成员变量age="+age);
super.show();
System.out.println("系别:
"+department);
}
}
publicclassApp8_3
{
publicstaticvoidmain(String[]args)
{
Studentstu=newStudent("李小四","信息系");
}
}
例8.4
//filename:
App8_4.java
classPerson
{
protectedStringname;
protectedintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
protectedvoidshow()
{
System.out.println("姓名:
"+name+" 年龄:
"+age);
}
}
classStudentextendsPerson
{
privateStringdepartment;
publicStudent(Stringname,intage,Stringdep)
{
super(name,age);
department=dep;
}
protectedvoidshow()
{
System.out.println("系别:
"+department);
}
}
publicclassApp8_4
{
publicstaticvoidmain(String[]args)
{
Studentstu=newStudent("王永涛",24,"电子");
stu.show();
}
}
例8.5
//filename:
App8_5.java
classPerson
{
protectedStringname;
protectedintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
protectedvoidshow()
{
System.out.println("姓名:
"+name+" 年龄:
"+age);
}
}
classStudentextendsPerson
{
privateStringdepartment;
publicStudent(Stringname,intage,Stringdep)
{
super(name,age);
department=dep;
}
protectedvoidshow()
{
System.out.println("系别:
"+department);
}
publicvoidsubshow()
{
System.out.println("我在之类中");
}
}
publicclassApp8_5
{
publicstaticvoidmain(String[]args)
{
Personper=newStudent("王永涛",24,"电子");
per.show();
//per.subshow();
}
}
例8.6
/*
父类中被声明为final的成员在子类中可被继承,但不能被覆盖。
filename:
App8_6.java
父类中的final方法不允许被覆盖;
*/
classAAA
{
staticfinaldoublePI=3.14;//声明静态常量
publicfinalvoidshow()//声明最终方法,无法被子类覆盖
{
System.out.println("pi="+PI);
}
}
classBBBextendsAAA
{
privateintnum=100;
publicvoidshow()//错误,不可以覆盖父类中的最终方法
{
System.out.println("num="+num);
}
}
publicclassApp8_6
{
publicstaticvoidmain(String[]args)
{
BBBex=newBBB();
ex.show();
}
}
例8.7
使用“==”和equals()比较对象的异同。
classA{
inta=1;
}
publicclassApp8_7{
publicstaticvoidmain(String[]args){
Aobj1=newA();
Aobj2=newA();
Strings1,s2,s3="abc",s4="abc"; //s3、s4指向同一字符串“abc”
s1=newString("abc");
s2=newString("abc");
System.out.println("s1.equals(s2)是"+(s1.equals(s2)));
System.out.println("s1==s3是"+(s1==s3));
System.out.println("s1.equals(s3)是"+(s1.equals(s3)));
System.out.println("s3==s4是"+(s3==s4));
System.out.println("s2.equals(s3)是"+(s2.equals(s3)));
System.out.println("s1==s2是"+(s1==s2));
System.out.println("obj1==obj2是"+(obj1==obj2));
System.out.println("obj1.equals(obj2)是"+(obj1.equals(obj2)));
obj1=obj2;
System.out.println("obj1=obj2后obj1==obj2是"+(obj1==obj2));
System.out.println("obj1=obj2后obj1.equals(obj2)是"+(obj1.equals(obj2)));
}
}
例8.8
/*
filename:
App8_8.java;
利用getClass()方法返回运行该方法的对象所属的类
*/
classPerson
{
protectedStringname;
publicPerson(Stringxm) //定义Person类的构造方法;
{
name=xm;
}
}
publicclassApp8_8 //主类
{
publicstaticvoidmain(String[]args)
{
Personper=newPerson("张三");
Classobj=per.getClass(); //利用对象per调用getClass()方法,返回per所属的类;
System.out.println("对象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());
System.out.print("Person.count="+this.count+"");
System.out.println("Student.count="+Student.count+"");
Objectobj=this;
if(objinstanceofStudent)//判断对象属于哪个类
System.out.println(obj.toString()+"是Student类对象。
");
elseif(objinstanceofPerson)
System.out.println(obj.toString()+"是Person类对象。
");
}
}
classStudentextendsPerson//子类Student继承父类Person,且是主类但不是public类
{
staticintcount=0;//隐藏了父类的count
protectedStringdept;
protectedStudent(Stringn1,inta1,Stringd1)
{
super(n1,a1);//调用父类的构造方法
dept=d1;
this.count++;//调用子类的静态变量
}
publicStringtoString()//覆盖父类的同名方法
{
returnsuper.toString()+","+dept;//调用父类的同名方法
}
publicvoiddisplay()
{
super.display();
System.out.println("super.count="+super.count);
System.out.println("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
{
protectedStringname;
publicShape(Stringxm)//抽象类中的一般方法,本方法是构造方法
{
name=xm;
System.out.print("名称:
"+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)
{
super(shapeName);
this.width=width;
this.height=height;
}
publicdoublegetArea()
{
returnwidth*height;
}
publicdoublegetlength()
{
return2*(width+height);
}
}
publicclassApp8_10
{
publicstaticvoidmain(String[]args)
{
Shaperect=newRectangle("长方形",6.5,10.3);
System.out.print(";面积="+rect.getArea()+";周长="+rect.getlength()+"\n");
Shapecircle=newCircle("圆",10.2);
System.out.print(";面积="+circle.getArea()+";周长="+circle.getlength());
}
}
程序运行结果及其分析:
例8.11利用形状接口IShape建造类。
//filenameApp8_11.java
interfaceIShape
{
finaldoublePI=3.14;
abstractdoublegetArea();
abstractdoublegetlength();
}
classCircleimplementsIShape
{
doubleradius;
publicCircle(doubler)
{
radius=r;
}
publicdoublegetArea()
{
returnPI*radius*radius;
}
publicdoublegetlength()
{
return2*PI*radius;
}
}
classRectangleimplementsIShape
{
privatedoublewidth;
privatedoubleheight;
publicRectangle(doublewidth,doubleheight)
{
this.width=width;
this.height=height;
}
publicdoublegetArea()
{
returnwidth*height;
}
publicdoublegetlength()
{
return2*(width+height);
}
}
publicclassApp8_11
{
publicstaticvoidmain(String[]args)
{
IShapecircle=newCircle(5.0);
System.out.print("圆的面积="+circle.getArea()+";周长="+circle.getlength()+"\n");
Rectanglerect=newRectangle(6.5,10.8);
System.out.print("矩形面积="+rect.getArea()+";周长="+rect.getlength());
}
}
例8.12接口的继承。
//filenameCylinder.java
interfaceFace1
{
finaldoublePI=3.14;
abstractdoublearea();
}
interfaceFace2
{
abstractvoidsetColor(Stringc);
}
interfaceFace3extendsFace1,Face2
{
abstractvoidvolume();
}
publicclassCylinderimplementsFace3
{
privatedoubleradius;
privateintheight;
protectedStringColor;
publicCylinder(doubler,inth)
{
radius=r;
height=h;
}
publicdoublearea()
{
returnPI*radius*radius;
}
publicvoidsetColor(Stringc)
{
Color=c;
System.out.println("颜色:
"+Color);
}
publicvoidvolume()
{
System.out.println("圆柱体体积:
"+area()*height);
}
publicstaticvoidmain(String[]args)
{
Cylindervolu=newCylinder(3.0,2);
volu.setColor("红色");
volu.volume();
}
}
例8.13利用接口实现类的多重继承。
//filenameCylinder.java
interfaceFace1
{
finaldoublePI=3.14;
ab
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 81815 实验 3238
![提示](https://static.bdocx.com/images/bang_tan.gif)