JAVA例78714及实验2631.docx

上传人:b****8 文档编号:10337825 上传时间:2023-02-10 格式:DOCX 页数:22 大小:95.41KB
下载 相关 举报
JAVA例78714及实验2631.docx_第1页
第1页 / 共22页
JAVA例78714及实验2631.docx_第2页
第2页 / 共22页
JAVA例78714及实验2631.docx_第3页
第3页 / 共22页
JAVA例78714及实验2631.docx_第4页
第4页 / 共22页
JAVA例78714及实验2631.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

JAVA例78714及实验2631.docx

《JAVA例78714及实验2631.docx》由会员分享,可在线阅读,更多相关《JAVA例78714及实验2631.docx(22页珍藏版)》请在冰豆网上搜索。

JAVA例78714及实验2631.docx

JAVA例78714及实验2631

例7.8将圆柱体类Cylinder里的变量pi和num声明为静态变量

编写程序如下:

//filename:

App7_8.java静态变量的使用

classCylinder//定义类Cylinder

{

privatestaticintnum=0;//声明num为静态变量

privatestaticdoublepi=3.14;//声明pi为静态变量,并赋初值

privatedoubleradius;

privateintheight;

publicCylinder(doubler,inth)//定义有两个参数的构造方法

{

radius=r;

height=h;

num++;//当构造方法Cylinder()被调用时,num便加1

}

publicvoidcount()//当count()方法用来显示目前创建对象的个数

{

System.out.print("创建了"+num+"个对象:

");

}

doublearea()

{

returnpi*radius*radius;

}

doublevolume()

{

returnarea()*height;

}

}

publicclassApp7_8//主类

{

publicstaticvoidmain(String[]args)

{

Cylindervolu1=newCylinder(2.5,5);

volu1.count();

System.out.println("圆柱1的体积="+volu1.volume());

Cylindervolu2=newCylinder(1.0,2);

volu2.count();

System.out.println("圆柱2的体积="+volu2.volume());

}

}

运行结果如下:

 

例7.9利用圆柱体类Cylinder来介绍静态方法的使用

程序编写如下:

//filename:

App7_9.java静态方法的使用

classCylinder//定义类Cylinder

{

privatestaticintnum=0;

privatestaticdoublepi=3.14;

privatedoubleradius;

privateintheight;

publicCylinder(doubler,inth)

{

radius=r;

height=h;

num++;//当构造方法Cylinder()被调用时,num便加1

}

publicstaticvoidcount()//当count()为静态方法

{

System.out.println("创建了"+num+"个对象");

}

doublearea()

{

returnpi*radius*radius;

}

doublevolume()

{

returnarea()*height;

}

}

publicclassApp7_9//主类

{

publicstaticvoidmain(String[]args)

{

Cylinder.count();

Cylindervolu1=newCylinder(2.5,3);

volu1.count();

System.out.println("圆柱1的体积="+volu1.volume());

Cylindervolu2=newCylinder(1.0,2);

Cylinder.count();

System.out.println("圆柱2的体积="+volu2.volume());

}

}

运行结果如下:

例7.10创建圆柱体类Cylinder,并对该类生成的对象进行赋值运算。

//filenameApp7_10.java

classCylinder//定义类Cylinder

{

privatestaticdoublepi=3.14;

privatedoubleradius;

privateintheight;

publicCylinder(doubler,inth)

{

radius=r;

height=h;

}

publicvoidsetCylinder(doubler,inth)

{

radius=r;

height=h;

}

doublevolume()

{

returnpi*radius*radius*height;

}

}

publicclassApp7_10//主类

{

publicstaticvoidmain(String[]args)

{

Cylindervolu1,volu2;//声明volu1,volu2两个引用型变量

volu1=newCylinder(2.5,5);//创建对象,并将volu1指向它

System.out.println("圆柱1的体积="+volu1.volume());

volu2=volu1;//将volu1赋值给volu2,volu2也指向了该对象

volu2.setCylinder(1.0,2);//重新设置圆柱的底半径和高

System.out.println("圆柱2的体积="+volu1.volume());

}

}

程序运行结果及其分析:

在该例的主方法main()中,声明了volu1,volu2两个Cylinder类型的变量,但只创建了一个对象volu1。

对于volu2,使用了赋值语句volu2=volu1,将两个不同名的引用变量指向同一个对象。

故通过任一个引用变量对对象进行修改,另一个引用变量所指向的对象内容也会随之更改。

例7.11以圆柱体类Cylinder的对象为参数进行方法调用,并说明对象的比较。

//filenameApp7_11.java

classCylinder//定义类Cylinder

{

privatestaticdoublepi=3.14;

privatedoubleradius;

privateintheight;

publicCylinder(doubler,inth)

{

radius=r;

height=h;

}

publicvoidcompare(Cylindervolu)//以对象作为方法的参数

{

if(this==volu)//判断this与volu是否指向同一对象

System.out.println("这两个对象相等");

else

System.out.println("这两个对象不相等");

}

}

publicclassApp7_11//主类

{

publicstaticvoidmain(String[]args)

{

Cylindervolu1=newCylinder(1.0,2);

Cylindervolu2=newCylinder(1.0,2);

Cylindervolu3=volu1;

pare(volu2);//调用compare(),比较volu1与volu2是否相等

pare(volu3);//调用compare(),比较volu1与volu3是否相等

}

}

程序运行结果及其分析:

在该例中,Cylinder类的compare()方法接收的参数是对象,并用if(this==volu)语句判断两个引用变量是否相等。

在主方法main()中,声明了三个引用变量volu1,volu2和volu3,并且用相同的实参创建了两个对象volu1和volu2。

然而volu1与volu2是两个彼此独立的对象,是两个占据不同内存空间地址的不同对象。

引用变量volu1与volu2的值分别是这两个对象在内存中的首地址,显然它们是不相等的;而volu1与volu3是指向同一个对象的两个变量,它们的值是同一对象在内存中的首地址,故两者相等。

例7.12创建个人类Person,在该类中定义一个以对象作为返回值的方法compare()。

//filenameApp7_12.java

classPerson//定义类Person

{

privateStringname;

privateintage;

publicPerson(Stringname,intage)

{

this.name=name;

this.age=age;

}

publicPersoncompare(Personp)//返回值的类型为对象

{

if(this.age>p.age)

returnthis;//返回值调用该方法的对象

else

returnp;//返回参数对象

}

}

publicclassApp7_12

{

publicstaticvoidmain(String[]args)

{

Personper1=newPerson("张三",20);

Personper2=newPerson("李四",21);

Personper3;

per3=pare(per2);

if(per3==per1)

System.out.println("张三年龄大");

else

System.out.println("李四年龄大");

}

}

程序运行结果及其分析:

该程序是通过比较两个对象的成员变量age的大小,来返回age的值较大的对象。

例7.13对象数组的应用。

以个人类Person为类型,创建数组。

//filenameApp7_13.java

classPerson

{

privateStringname;

privateintage;

publicPerson(Stringname,intage)

{

this.name=name;

this.age=age;

}

publicvoidshow()

{

System.out.println("姓名:

"+name+"年龄:

"+age);

}

}

publicclassApp7_13

{

publicstaticvoidmain(String[]args)

{

Person[]per;//声明类类型的数据

per=newPerson[3];//用new运算符为数组分配内存空间

per[0]=newPerson("张三",20);//用new运算符创建新对象,并分配给数组元素

per[1]=newPerson("李四",21);

per[2]=newPerson("王二",19);

per[2].show();//利用对象per[2]调用show()方法

per[0].show();

}

}

程序运行结果及其分析:

在主方法main()中,定义了包含3个元素的数组per,其类型为Person类类型,即数组元素是Person类型的变量,所以程序中分别用数组元素指向新建对象的内存地址。

用不同的对象调用相应的show()方法,则显示出相应对象的成员数据。

例7.14以对象组作为参数传递给方法,返回对象数组中最小的成员变量。

//filenameApp7_14.java

classPerson

{

privateStringname;

privateintage;

publicPerson(Stringname,intage)

{

this.name=name;

this.age=age;

}

publicstaticintminAge(Person[]p)//以对象数组作为参数传递给方法

{

intmin=Integer.MAX_VALUE;

for(inti=0;i

if(p[i].age

min=p[i].age;//将对象数组中成员变量age的最小值存入变量min中

returnmin;//返回对象数组中最小的成员变量的值

}

}

publicclassApp7_14

{

publicstaticvoidmain(String[]args)

{

Person[]per=newPerson[3];

per[0]=newPerson("张三",20);

per[1]=newPerson("李四",21);

per[2]=newPerson("王二",19);

System.out.println("最小的年龄为:

"+Person.minAge(per));

}

}

程序运行结果及其分析:

实验26类的私有成员与公共成员

实验要求:

编写一个Java程序,在程序中定义一个Person类,其中age属性是一个私有属性,并且范围在5~20之间

classPerson

{

Stringname;

privateintage;

publicbooleansetAge(intnewAge)

{

if(5<=newAge&&newAge<=20)

{

age=newAge;

return(true);

}

else

return(false);

}

publicintgetAge()

{

return(age);

}

}

publicclassApp

{

publicstaticvoidmain(String[]args)

{

Persons1=newPerson();

s1.name="张三";

if(s1.setAge(25))

System.out.println("姓名:

"+s1.name+",年龄:

"+s1.getAge());

else

System.out.println("年龄错误");

}

}

运行结果如下:

实验27类的构造方法1

实验要求:

编写一个Java程序,在程序中定义Student类,Student类有三个构造方法,分别对不同的属性进行初始化

编写程序如下

classStudent//定义类Student.

{

Stringname;

intage;

publicStudent()//定义无参的构造方法。

{

System.out.println("Student()构造方法被调用");

}

publicStudent(Stringc)//定义有一个参数的构造方法。

{

name=c;

System.out.println("Student(StringnewName)构造方法被调用");

}

publicStudent(Stringa,intb)//定义有两个参数的构造方法。

{

name=a;

age=b;

System.out.println("Student(StringnewName,intnewAge)构造方法被调用");

}

publicstaticvoidmain(String[]args)

{

Studentvolu1=newStudent();//创建Student类的一个对象,不传入参数

Studentvolu2=newStudent("张三");

//创建Student类的一个对象,传入一个参数:

”张三”

Studentvolu3=newStudent("张三",15);

//创建Student类的一个对象,传入两个参数:

”张三”、15

}

}

运行结果如下

程序分析如下:

程序中的Student类有三个构造方法,分别对不同的属性进行初始化。

实验28类的构造方法2

实验要求:

编写一个Java程序,在程序中定义一个Employee类,Employee类有四个构造方法,分别对不同的属性进行初始化,有一个setAge()方法能设置age属性值,有一个getAge()方法能返回age属性值

程序编写如下:

classEmployee

{

privateStringname;

privateintage;

privatedoublesalary;

publicEmployee(StringnewName,intnewAge,doublenewSalary)

{

this(newAge,newSalary);

System.out.println("三个参数的构造方法被调用");

name=newName;

}

publicEmployee(intnewAge,doublenewSalary)

{

this(newSalary);

System.out.println("两个参数的构造方法被调用");

age=newAge;

}

publicEmployee(doublenewSalary)

{

this();

System.out.println("一个参数的构造方法被调用");

salary=newSalary;

}

publicEmployee()

{

System.out.println("没有参数的构造方法被调用");

}

voidsetAge(intnewAge)

{

intage=0;

this.age=newAge;

}

intgetAge()

{

returnage;

}

publicstaticvoidmain(String[]args)

{

Employees1=newEmployee("李四",30,8000);

s1.setAge(31);

System.out.println("年龄是"+s1.getAge());

}

}

运行结果如下:

实验29编写一个java程序,在程序中定义一个MyPlus类,MyPlus类有三个plus()方法,分别对不同的参数进行求和。

//MyPlus.java

classMyPlus

{

publicintplus(inta,intb)

{

ints;

s=a+b;

returns;

}

publicintplus(inta,intb,intc)

{

ints;

s=plus(a,b)+c;

returns;

}

publicintplus(inta,intb,intc,intd)

{

ints;

s=plus(a,b,c)+d;

returns;

}

publicstaticvoidmain(String[]args)

{

MyPlusp=newMyPlus();

System.out.println("1+2="+p.plus(1,2));

System.out.println("1+2+3="+p.plus(1,2,3));

System.out.println("1+2+3+4="+p.plus(1,2,3,4));

}

}

程序运行结果:

实验30编写一个java程序,在程序中定义一个Ca类,Ca类有静态成员和非静态成员,再定义一个Cb类,在Cb类中创建Ca类的对象。

//Ca.java

classCa

{

inta;

staticintb;

publicstaticvoidm1()

{

inta=10;

intb=10;

}

publicvoidm2()

{

a=5;

b=5;

}

publicvoidm3()

{

m1();

m2();

}

}

classCb

{

publicstaticvoidmain(String[]args)

{

Cas1=newCa();

Cas2=newCa();

Ca.b=5;

s1.b=6;

s2.b=7;

System.out.println("Ca.b="+Ca.b);

System.out.println("s1.b="+s1.b);

System.out.println("s2.b="+s2.b);

}

}

实验31编写一个java程序,在程序中定义一个StudentA类,生成StudentA类的两个对象,分别对这些对象进行赋值,再进行比较。

//StudentA.java

publicclassStudentA

{

StringstrName;

publicstaticvoidmain(String[]args)

{

StudentAa=newStudentA();

a.setName("张三");

StudentAb=a;

b.setName("李四");

StudentAc=newStudentA();

c.setName("王五");

System.out.println("a的名字是"+a.getName());

System.out.println("b的名字是"+b.getName());

System.out.println("c的名字是"+c.getName());

if(a==b)

System.out.println("a与b相等");

else

System.out.println("a与b不相等");

if(a==c)

System.out.println("a与c相等");

else

System.out.println("a与c不相等");

}

publicvoidsetName(Stringname)

{

this.strName=name;

}

publicStringgetName()

{

returnstrName;

}

}

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

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

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

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