一、上机目的
1.掌握类的声明,对象的创建以及方法的定义和调用。
2.掌握包机制。
3.掌握类的继承。
4. 掌握多态机制。
5.掌握抽象类与接口的使用。
二、上机内容
1.(1)设计一个矩形类Rectangle,该类包含2个私有实例变量:矩形的长(length)和宽(width),它还有一个方法,计算长方形的面积getArea()。在Rectangle类中创建构造方法,初始化类中的各个变量;
(2) 设计一个长方体类Cuboid,该类包含2个私有实例变量:长方体的底面矩形(rectangle)和高(height),它还有一个方法,计算长方体的体积getVolume()。在Cuboid类中创建构造方法,初始化类中的各个变量;
(3)设计一个测试类Test,该类中分别创建类Rectangle和Cuboid的实例,求矩形的面积和长方体的体积。
Rectangle类:
public class Rectangle {
private double length;
private double width;
public Rectangle() {
this.length=0;
this.width=0;
}
public Rectangle(double length,double width) {
this.length=length;
this.width=width;
}
public double getArea() {
return length*width;
}
}
Cuboid类:
public class Cuboid {
private double rectangle;
private double height;
public Cuboid() {
this.rectangle=0;
this.height=0;
}
public Cuboid(double rectangle,double height) {
this.rectangle=rectangle;
this.height=height;
}
public double getVolume() {
return rectangle*