实验报告面向计算机对象编程.docx
- 文档编号:10870616
- 上传时间:2023-02-23
- 格式:DOCX
- 页数:18
- 大小:87.18KB
实验报告面向计算机对象编程.docx
《实验报告面向计算机对象编程.docx》由会员分享,可在线阅读,更多相关《实验报告面向计算机对象编程.docx(18页珍藏版)》请在冰豆网上搜索。
实验报告面向计算机对象编程
实验二:
面向对象编程
实验目的:
通过编程和上机实验理解Java语言是如何体现面向对象编程基本思想,熟悉类的封装方法以及如何创建类和对象,熟悉成员变量和成员方法的特性,熟悉类的继承性和多态性的作用,熟悉包、接口的使用方法,掌握OOP方式进行程序设计的方法。
实验要求:
1、编写程序实现类的定义和使用。
2、编写不同成员和不同成员方法修饰方法的程序。
3、编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序多态性(成员方法重载、构造方法重载)的程序。
4、编写接口的定义和使用的程序。
5、编写包的定义和使用的程序。
实验内容:
1、类的定义和使用
(1)定义一个满足如下要求的Date类:
a.用下面的格式输出日期:
日/月/年
b.可运行在日期上加一天操作
c.设置日期
用该类编写一个显示当前日期的程序。
源程序:
importjava.util.Calendar;
publicclassDate
{
privateintyear,month,day;//私有成员变量
privatestaticintthisYear=2011;//当前年份,私有静态成员变量
static//静态成员变量初始化
{
thisYear=2011;
}
publicDate(intyear,intmonth,intday)//构造方法,指定日期
{
this.set(year,month,day);//调用本类的成员方法
}
publicDate()//默认构造方法,缺省日期,重载
{
this(1970,1,1);//调用本类已声明的其他构造方法
}
publicDate(Dated)//拷贝构造方法,日期同参数,重载
{
this.set(d);
}
publicvoidset(inty,intm,intd)//公有成员方法,设置日期值,算法不全
{
this.year=y;//对成员变量赋值
this.month=((m>=1)&(m<=12))?
m:
1;
this.day=((d>=1)&(d<=31))?
d:
1;
}
publicvoidset(Dated)//设置日期值,重载
{
set(d.year,d.month,d.day);//调用同名成员方法,不能使用this()
}
publicintgetYear()//获得年份
{
returnthis.year;
}
publicintgetMonth()//获得月份
{
returnthis.month;
}
publicintgetDay()//获得当月日期
{
returnthis.day;
}
publicStringtoString()//返回中文日期字符串
{
returnthis.year+"/"+this.month+"/"+this.day+"/";
}
publicstaticintgetThisYear()//获得今年年份,静态方法
{
returnthisYear;//访问静态成员变量
}
publicstaticbooleanisLeapYear(intyear)//判断指定年份是否闰年,静态方法
{
returnyear%400==0||year%100!
=0&&year%4==0;
}
publicbooleanisLeapYear()//判断当前日期的年份是否闰年,重载
{
returnisLeapYear(this.year);//调用静态方法
}
publicbooleanequals(Dated)//比较当前日期值与d是否相等
{//this指代调用本方法的当前对象
returnthis==d||d!
=null&&this.year==d.year&&this.month==d.month&&this.day==d.day;
}
publicstaticintdaysOfMonth(intyear,intmonth)//返回指定年月的天数,静态方法
{
switch(month)//计算每月的天数
{
case1:
case3:
case5:
case7:
case8:
case10:
case12:
return31;
case4:
case6:
case9:
case11:
return30;
case2:
returnisLeapYear(year)?
29:
28;
default:
return0;
}
}
publicintdaysOfMonth()//返回当月天数
{
returndaysOfMonth(this.year,this.month);
}
publicvoidtomorrow()//当前日期改为第二天日期
{
this.day++;//通过this改变当前对象的值,没有返回值
if(day>this.daysOfMonth())
{
day=1;
month++;
if(month>12)
{
month=1;
year++;
}
}
}
publicDateyestoday()//返回当前日期的前一天日期
{
Dateyes=newDate(this);//执行拷贝构造方法,创建新实例,没有改变当前对象this
yes.day--;
if(yes.day==0)
{
yes.month--;
if(yes.month==0)
{
yes.month=12;
yes.year--;
}
yes.day=daysOfMonth(yes.year,yes.month);
}
returnyes;//返回对象引用
}
publicstaticintthisyear(){
returnCalendar.getInstance().get(Calendar.YEAR);//返回当年的年份
}
publicstaticvoidmain(Stringargs[])
{
System.out.println("当前时间"+Calendar.getInstance().get(Calendar.YEAR)+"/"+Calendar.getInstance().get(Calendar.MONTH)+"/"+Calendar.getInstance().get(Calendar.DATE));
System.out.println("今年是"+Date.getThisYear());
Dated1=newDate(2011,3,24);
Dated2=newDate(d1);//调用拷贝构造方法复制实例
System.out.println(d1.toString());
System.out.print(d2+"的昨天是"+d2.yestoday()+"\n"+d2+"的明天是");//yestoday()方法返回对象,没有改变d2
d2.tomorrow();//改变d2对象值
System.out.println(d2.toString());
}
运行结果:
(2)创建一个桌子Table类,该类中有桌子名称、重量、桌面宽度、长度及桌子高度属性。
其中有:
a.构造函数初始化所有数据成员
b.Area():
计算桌面的面积
c.Display():
在屏幕上输出所有数据成员的值
d.ChangeWeight(int):
改变桌子重量的函数
e.在main()中实现创建一个桌子对象,计算桌面的面积,改变桌子重量,并在屏幕上输出所有桌子数据成员的值。
源程序:
publicclassTable{
privatedoubletablename;
privatedoubletableweight;
privatedoubletablewidth;
privatedoubletablelength;
privatedoubletablehigth;
publicdoublegetTablename(){
returntablename;
}
publicvoidsetTablename(doubletablename){
this.tablename=tablename;
}
publicdoublegetTableweight(){
returntableweight;
}
publicvoidsetTableweight(doubletableweight){
this.tableweight=tableweight;
}
publicdoublegetTablewidth(){
returntablewidth;
}
publicvoidsetTablewidth(doubletablewidth){
this.tablewidth=tablewidth;
}
publicdoublegetTablelength(){
returntablelength;
}
publicvoidsetTablelength(doubletablelength){
this.tablelength=tablelength;
}
publicdoublegetTablehigth(){
returntablehigth;
}
publicvoidsetTablehigth(doubletablehigth){
this.tablehigth=tablehigth;
}
publicTable(doubletablename,doubletableweight,doubletablewidth,
doubletablelength,doubletablehigth){
super();
this.tablename=tablename;
this.tableweight=tableweight;
this.tablewidth=tablewidth;
this.tablelength=tablelength;
this.tablehigth=tablehigth;
}
publicdoubleArea(){
return(this.tablewidth*this.tablelength);
}
publicvoidChangeWeight(doubleweigth){
this.setTableweight(weigth);
}
publicvoidDisplay(){
System.out.println("桌子宽度是:
"+getTablewidth()+"\n桌子长度是:
"+getTablelength()+"\n桌子高度是:
"+getTablehigth()+"\n桌面面积是:
"+Area()+"\n改变重量为:
"+getTableweight());
}
publicstaticvoidmain(Stringargs[]){
Tabletable=newTable(1.0,2.0,3.0,4.0,5.0);
System.out.println("桌子重量"+table.getTableweight());
table.ChangeWeight(15.5);
table.Display();
}
}
运行结果:
2、修饰符的使用:
有时需要公开一些变量和方法,有时需要禁止其他对象使用变量和方
法,这时可以使用修饰符来实现这个目的。
常用的修饰符如下。
Public,private,protected,package,static,final,transient,volatile。
程序功能:
通过两个类StaticDemo、TestDemo说明静态变量/方法与实例变量/方法的区别。
程序源代码:
classStaticDemo{
staticintx;
inty;
publicstaticintgetX(){
returnx;
}
publicstaticvoidsetX(intnewX){
x=newX;
}
publicintgetY(){
returny;
}
publicvoidsetY(intnewY){
y=newY;
}
}
publicclassTestDemo{
publicstaticvoidmain(String[]args){
System.out.println("静态变量x="+StaticDemo.getX());
System.out.println("实例变量y="+newStaticDemo().getY());//静态方法里不能调用非静态方法,非法,编译时将出错
StaticDemoa=newStaticDemo();
StaticDemob=newStaticDemo();
a.setX
(1);
a.setY
(2);
b.setX(3);//静态方法,赋值会覆盖上次的值
b.setY(4);
System.out.println("静态变量a.x="+a.getX());
System.out.println("实例变量a.y="+a.getY());
System.out.println("静态变量b.x="+b.getX());
System.out.println("实例变量b.y="+b.getY());
}
}
运行结果:
3、继承和多态的作用
创建Rodent(啮齿动物):
Mouse(老鼠),Gerbil(沙鼠),Hamster(大颊鼠)等的一个继承分级结构。
在基础类中,提供适用于所有Rodent的方法,并在衍生类中覆盖它们,从而根据不同类型的Rodent采取不同的行动。
创建一个Rodent数组,在其中填充不同类型的Rodent,然后调用自己的基础类方法,看看会有什么情况发生。
源程序:
classRodent
{
publicvoidrun()
{
System.out.println("啮齿动物跑得快");
}
}
classMouseextendsRodent
{
publicvoidrun()
{
System.out.println("老鼠跑得快");
}
}
classGerbilextendsRodent
{
publicvoidrun()
{
System.out.println("沙鼠在哪跑得快?
");
}
}
classHamsterextendsRodent
{
publicvoidrun()
{
System.out.println("大颊鼠怎么跑?
");
}
}
publicclassRodent_test
{
publicstaticvoidmain(Stringargs[])
{
Rodent[]a=newRodent[]{newRodent(),newMouse(),newGerbil(),newHamster()};
Rodentrodent=newRodent();
Mousemouse=newMouse();
Gerbilgerbil=newGerbil();
Hamsterhamster=newHamster();
a[0]=rodent;
a[1]=mouse;
a[2]=gerbil;
a[3]=hamster;
for(inti=0;i { a[i].run(); } } } 运行结果: 4、接口的定义和使用 源程序: interfacecal_area{ doublePI=3.14; doublecalculate_area(); } classCircleimplementscal_area{ doubler; Circle(){} publicvoidcircle(doubler){ this.r=r; } publicdoublecalculate_area(){ return(PI*r*r); } } publicclassInterfaceTest{ publicstaticvoidmain(Stringargs[]){ doublex; Circley=newCircle(); y.circle(2.0); x=y.calculate_area(); System.out.println("\n面积为: "+x+"\n"); } } 运行结果: 5、包的定义和使用 在包中创建类YMD.java程序功能: 在源程序中,首先声明使用的包名Mypackage,然后创建YMD类,该类具有计算今年的年份并输出一个带有年月日的字符串的功能。 源代码: packageMypackage;//声明存放类的包 importjava.util.*;//引用java.util包 publicclassTest_YMD{ privateintyear,month,day; publicstaticvoidmain(String[]arg3){} publicTest_YMD(inty,intm,intd){ year=y; month=(((m>=1)&(m<=12))? m: 1); day=(((d>=1)&(d<=31))? d: 1); } publicTest_YMD(){ this(0,0,0); } publicstaticintthisyear(){ returnCalendar.getInstance().get(Calendar.YEAR);//返回当年的年份 } publicintyear(){ returnyear;//返回年份 } publicStringtoString(){ returnyear+"-"+month+"-"+day;//返回转化为字符串的年-月-日 } } 编写使用包Mypackage中Test_YMD类的程序YMD_2.java程序功能: 给定某人姓名与出生日期,计算该人年龄,并输出该人姓名,年龄,出生日期。 程序使用了Test_YMD的方法来计算年龄。 源代码: packageMypackage; importMypackage.Test_YMD;//引用Mypackage包中的KY4_1_YMD类 publicclassYMD_2{ privateStringname; privateTest_YMDbirth; publicstaticvoidmain(Stringargs[]){ YMD_2a=newYMD_2("张驰",1990,1,11); a.output(); } publicYMD_2(Stringn1,Test_YMDd1) { name=n1; birth=d1; } publicYMD_2(Stringn1,inty,intm,intd) { this(n1,newTest_YMD(y,m,d));//初始化变量与对象 } publicintage()//计算年龄 { returnTest_YMD.thisyear()-birth.year();//返回当前年与出生年的差即年龄 } publicvoidoutput() { System.out.println("姓名: "+name); System.out.println("出生日期: "+birth.toString()); System.out.println("今年年龄: "+age()); } } 运行结果:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 报告 面向 计算机 对象 编程
![提示](https://static.bdocx.com/images/bang_tan.gif)