实验二 类与对象
一、 实验目的
(1) 掌握面向对象程序设计的方法和Java作为面向对象程序设计语言的特点; (2) 掌握类得封装与对象的使用;
(3) 掌握修饰符和构造方法的使用规则; (4) 掌握构造方法的使用,方法重载用于多态 (5) 掌握类的继承,方法重构与变量的隐藏 (6) 掌握接口的特点、结构、调用和继承; (7) 掌握如何创建包,通过包如何管理类; (8) 掌握Java的继承机制和实现多态的方法。
二、 实验要求
(1)编写一个体现面向对象思想的程序;
(2)编写一个创建对象和使用对象的方法的程序;
(3)编写体现类的继承性(成员变量、成员方法、成员变量隐藏)的程序; (4)编写体现类的多态性(成员方法重载、构造方法重载)的程序。 (5)编写一个接口,并使用它; (6)编写自定义包的程序
三、 实验内容
(一)Java类的设计与编程
1. 创建一个 Application 应用程序 Demo2_1.java 文件。在文件中定义两个类。一个是CCircle,另一个是主类Demo2_1。
程序功能:描述一个圆类,其主要属性为半径,还有一个非常重要的常量是圆周率。该圆可以求出面积,可以设置半径和取得半径大小。
class CCircle{
double pi; double radius; double getRadius(){
return radius;
}
void setCircle(double r, double p){
pi=p;
radius=r; }
double getCircleArea() {
return pi*radius*radius; } }
public class Demo2_1{
public static void main(String args[]){
CCircle cir1=new CCircle();
cir1.setCircle(2.0,3.1416);
System.out.println(\圆半径=\ System.out.println(\圆面积=\ cir1.pi=3.14159; cir1.radius=10.0;
System.out.println(\圆半径=\ System.out.println(\圆面积=\ } }
【思考】
① 试述程序中两个类的各自作用。
② 类CCircle都封装了什么?求圆面积的初始数据是通过那个方法实现的?请修改程序采
用构造函数的方法初始化数据。
③ 将class CCircle中的变量 double pi; double radius;加上private修饰符,进行编
译会出现什么问题,为什么? ④ 将void setCircle(double r, double p),double getRadius()和double getCircleArea()
加上修饰符static,重新编译会出现什么问题?仍用static,怎样修改别的地方来纠正这个错误?在main中引用这些方法时不用创建对象则怎样引用?
(二)类的继承设计
1.下面是一个具有继承的程序RectangleDemo.java。类Volume继承了父类Rectangle,主类是RectangleDemo。
public class RectangleDemo{
public static void main (String[] args){
double result;
Volume obj1=new Volume (10,20,30); result=obj1.volume (); System.out.println (\得到的长方体体积 =\result=obj1.area ();
System.out .println (\得到的长方形面积 =\
result=obj1.volume (10.0);
System.out.println (\立方体的体积=\
}
}
class Rectangle{
double length; double width; double area() {
return length*width;
}
Rectangle(double w,double l) {
length=l;
width=w; } }
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; } } 【思考】
① 类RectangleDemo起何作用?Volume类和Rectangle类之间有何关系?
② 解释类Volume中 Volume(double l, double w, double h) 的作用,super(l,w) 句的作用;
语
③ 指出类Volume中方法 double volume()和double volume(double y)体现了面向对
象技术的哪个性质?
④ 如果对类Rectangle中的方法 double area()加上修饰符private,程序能否通过编
译,为什么? ⑤ 写出程序运行结果;
2. 文件名为Demo2_3.java,该程序具有继承、成员的覆盖与隐藏概念。请运行该程序然后分析。
class Father{
int x=10,y=20; void speak(){
}
void calculate(String s) {
} }
class Son extends Father{
int x=30; void speak(){ }
void this_super(){ } }
public class Demo2_3{
public static void main(String args[]){
super.x=100; //修改父类中的x,y值 super.y=200; super.speak(); this.speak();
calculate(\在子类中直接调用\验证父类中的值已被修改 x=200;//this.x=200;
y=300;//this.y=300;向父类追踪,相当super.y System.out.println(\System.out.println(s+\System.out.println(\
Father father=new Father(); Son son=new Son();
System.out.println(\调用father对象speak()和calculate()结果-----\ father.speak();
father.calculate (\用father对象调用计算\ 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对象调用\
System.out.println(\ } } 【思考】
①子类Son中隐藏了父类Father那个变量?覆盖了那个方法?
②在子类Son方法void this_super()中,语句super.speak();this.speak();各有何作用? 输出什么结果?
③在子类Son方法void this_super()中,语句super.x=100; super.y=200; 修改的是哪个类x,y值?之后用什么方法验证的,结果应是什么?
④在子类Son方法void this_super()中,语句x=200; y=300; 修改的是哪个类x,y值? ⑤用对象father.speak()和son.speak()与类定义中的super.speak();this.speak();有何不同? ⑥参见[运行结果]逐项分析程序得出结果的原因。
(三)使用修饰符
有时需要公开一些变量和方法,有时需要禁止其他对象使用变量和方法,这时可以使用修饰符来实现这个目的。常用的修饰符如下。public,private,protected,package,static,final,transient,volatile
程序功能:通过两个类StaticDemo、KY2_4 说明静态变量/方法与实例变量/方法的区别。
编写类文件Demo2_4.java,程序源代码如下:
class StaticDemo {
static int x; int y;
public static int getX() {
return x; }
public static void setX(int newX) {
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库实验2 类与对象在线全文阅读。
相关推荐: