《java实验继承与多态初步》由会员分享,可在线阅读,更多相关《java实验继承与多态初步(5页珍藏版)》请在金锄头文库上搜索。
1、1实验 3、继承与多态初步1. 实验目的1). 进一步理解 Java 语言是如何体现面向对象编程基本思想。2). 初步理解继承和派生的概念与使用。3). 初步理解多态的概念与使用。实验器材和实验环境:计算机,网络环境,投影设备。实验相关软件:Window XP , Java 软件开发工具包(JSDK)。3*预习及知识准备预习课本第 5 章内容. 3实验内容:1). 调试下面的程序,指出程序运行结果,并回答下列问题在 shyan.java 文件中定义两个类。一个是 CCircle,另一个是主类 shyan。class CCircle double pi; double radius;double
2、 getRadius() return radius; void setCircle(double r, double p) pi=p; radius=r; double getCircleArea() return pi*radius*radius;public class Shyan2 public static void main(String args) CCircle cir1=new CCircle(); cir1.setCircle(2.0,3.1416); System.out.println(园半径=+cir1.getRadius();System.out.println(园
3、面积=+cir1.getCircleArea(); cir1.pi=3.14159;cir1.radius=10.0;System.out.println(园半径=+cir1.getRadius();System.out.println(园面积=+cir1.getCircleArea(); 【回答下列问题】 试述程序中两个类的各自作用。 类 CCircle 都封装了什么?求园面积的初始数据是通过那个方法实现的?请修改程序采用构造函数的方法初始化数据。 将 class CCircle 中的变量 double pi; double radius;加上 private 修饰符,进行编译会出现什么问题
4、,为什么? 将 void setCircle(double r, double p), double getRadius()和 double getCircleArea()加上修饰符 static,重新编译会出现什么问题?仍用 static,怎样修改别的地方来纠正这个错误?在 main 中引用这些方法时不用创建对象则怎样引用?2). 调试下面的程序,指出程序运行结果,并回答下列问题下面是一个具有继承的程序 RectangleDemo.java。类 Volume 继承了父类Rectangle,主类是 RectangleDemo。public class RectangleDemo public
5、static void main (String args) double result;Volume obj1=new Volume (10,20,30); result=obj1.volume (); System.out.println (得到的长方体体积 =+result);result=obj1.area (); System.out .println (得到的长方形面积 =+result);result=obj1.volume (10.0);System.out.println (立方体的体积=+result); class Rectangle double length;doub
6、le width; Rectangle(double w,double l) length=l;3width=w;double area() return length*width; class Volume extends Rectangle double height;Volume(double l,double w,double h) super(l,w); height=h;double volume() double vol;vol=area()*height; return vol;double volume(double y) return y*y*y; 【回答下列问题】类 Re
7、ctangleDemo 起何作用?Volume 类 和 Rectangle 类之间有何关系?解释类 Volume 中 Volume(double l,double w,double h) 的作用,super(l,w) 语句的作用;指出类 Volume 中方法 double volume()和 double volume(double y)体现了面向对象技术的哪个性质?如果对类 Rectangle 中的方法 double area()加上修饰符 private,程序能否通过编译,为什么?3). 调试下面的程序,指出程序运行结果,并回答下列问题下面的程序具有继承、成员的覆盖与隐藏概念。请先调试该程
8、序然后分析。class Father int x=10,y=20;void speak() System.out.println(I am Father:+x=+x+:y=+y); void calculate(String s) System.out.println(s+x*y=+x+*+y+=+x*y); class Son extends Father4 int x=30;void speak()System.out.println(I am Son:+x=+x+:y=+y);void this_super() super.x=100; /修改父类中的 x,y 值super.y=200;
9、super.speak();this.speak();calculate(在子类中直接调用);/验证父类中的值已被修改x=200;/this.x=200;y=300;/this.y=300;向父类追踪,相当 super.ypublic class shyansan public static void main(String args) Father father=new Father();Son son=new Son(); System.out.println(-调用 father 对象 speak()和 calculate()结果-); father.speak();father.cal
10、culate (用 father 对象调用计算);/x*y=200 System.out.println(-调用 son 对象 speak 方法-);son.speak();System.out.println(-调用 son 对象的修改数据方法-);son.this_super(); /用 son 对象中的方法 System.out.println(-调用 father 对象计算方法-);father.calculate(修改后用 father 对象调用); System.out.println(-调用 son 对象计算方法-);son.calculate(修改后用 son 对象调用);/x
11、*y=100*300System.out.println(I am main:+son.x=+son.x+:son.y=+son.y);【回答下列问题】子类 Son 中隐藏了父类 Father 那个域变量?覆盖了那个方法? 在子类 Son 方法 void this_super()中,语句 super.speak();this.speak();各有何作用? 输出什么结果?在子类 Son 方法 void this_super()中,语句 super.x=100; super.y=200; 修改的是哪个类x,y 值?之后用什么方法验证的,结果应是什么?5在子类 Son 方法 void this_super()中,语句 x=200; y=300; 修改的是哪个类 x,y 值?用对象 father.speak()和 son.speak()与类定义中的 super.speak();this.speak();有何不同?*4 课后练习(课堂上选做)调试下列程序,指出程序实现的功能及程序输出结果。1)调试课本第 5 章的例程。5按要求写出实验报告根 据 实 验 的 真 实 情 况 , 进 行 分 析 , 写 出 实 验 报 告 。( 实验报告格式要求 见附录 2)