您的当前位置:首页正文

类与对象实验

来源:个人技术集锦
-----WORD格式--可编辑--专业资料-----

《Java程序设计》 实验报告 实验名称 类与对象 实验室 实验楼521 实验日期 2012-03-22 类与对象 一、实验目的 1、 掌握类的定义和使用,编写构造方法及成员方法。 2、 能够创建类的实例,掌握对象的声明和不同访问属性的成员访问方式。 3、 会定义接口、实现接口。 二、实验内容 1. 设计并实现一个课程类,代表学校中的一门课程,将这门课程的相关信息组成该类的属性(如课程代码、课程名称、课程类别、学时、学分等),并在类中定义各个属性相关的访问方法(如获取和设置课程代码、课程名称,课程类别,获取和修改学时、学分,打印输出课程基本信息等),最后使用主函数测试此类(包括创建对象,调用相应方法等)。 2. 在java中,定义一个接口,声明计算图形面积和周长的抽象方法,再用类去实现这个接口,再编写一个测试类去使用这个接口。 三、实验环境 JDK 四、实验步骤 实验一: 1、定义一个Course类: --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- 2、对相关信息输出操作: 3、对相关函数进行调用: --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- 实验二: 1、定义一个Client类: --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- 2、编写计算周长和面积的函数: 3、定义一个MyRectangle类:在类中定义相关的函数 --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- 五、实验结果 实验一: --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- 实验二: 六、小结 通过本次实验,基本了解了类与对象的概念和基本定义方法,在测试时仍然存在一些问题,通过对错误书写的检查和修改,程序可以正常运行!在试验过程中基本掌握了类的定义和使用,编写构造方法及成员方法。能够创建类的实例,掌握对象的声明和不同访问属性的成员访问方式。会定义接口、实现接口。 七、源程序清单 1、 class Course{ int Cnumber; String Cname; String Ctype; int Ctime; int Cscrose; Course(int Cnu,String Cna,String Cty,int Ct,int Cs){ Cnumber=Cnu; Cname=Cna; Ctype=Cty; Ctime=Ct; Cscrose=Cs; } void display1(){ System.out.println(\"课程号:\"+Cnumber); } --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- void change1(int Cnu){ Cnumber=Cnu; } void display2(){ System.out.println(\"课程名称:\"+Cname); } void change2(String Cna){ Cname=Cna; } void display3(){ System.out.println(\"课程类型:\"+Ctype); } void change3(String Cty){ Ctype=Cty; } void display4(){ System.out.println(\"课时:\"+Ctime); } void change4(int Ct){ Ctime=Ct; } void display5(){ System.out.println(\"课程学分:\"+Cscrose); } void change5(int Cs){ Cscrose=Cs; } } public class Lesson{ public static void main(String []args){ Course human=new Course(1,\"English\必修\ human.display1(); human.display2(); human.display3(); human.display4(); human.display5(); --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- human.Cnumber=2; human.Cname=new String(\"Math\"); human.Ctype=new String(\"必修\"); human.Ctime=36; human.Cscrose=5; System.out.println(\"修改后的课程信息:\"); human.display1(); human.display2(); human.display3(); human.display4(); human.display5(); } } } 2、 public class Client{ public static void main(String []args){ ICalculate cal=new Calculate(); MyRectangle rect=new MyRectangle(10,5); System.out.println(rect+\"周长为:\"+cal.calcuGirth(rect)); System.out.println(rect+\"面积为:\"+cal.calcuArea(rect)); rect=new MyRectangle(30,50); System.out.println(rect+\"周长为:\"+cal.calcuGirth(rect)); System.out.println(rect+\"面积为:\"+cal.calcuArea(rect)); } } interface ICalculate{ int calcuArea(MyRectangle rect); int calcuGirth(MyRectangle rect); } class Calculate implements ICalculate{ //计算面积的方法 public int calcuArea(MyRectangle rect){ int result=rect.getWidth()*rect.getHeight(); return result; } //计算周长的方法 public int calcuGirth(MyRectangle rect){ int result=(rect.getWidth()+rect.getHeight())*2; return result; } } class MyRectangle{ private int width; --完整版学习资料分享----

-----WORD格式--可编辑--专业资料----- private int height; public MyRectangle(int width ,int height){ this.width=width; this.height=height; } public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public void setHeight(int height){ this.height=height; } public int getHeight(){ return height; } public String toString(){ return super.toString()+\"[width=\"+width+\ } }

--完整版学习资料分享----

因篇幅问题不能全部显示,请点此查看更多更全内容