实验四 继承和接口
实验四 继承和接口
一、实验目的
1.能实现类的继承关系;
2.用多种方法创建各个类的对象;
二、实验内容
完成如下任务或编写能够满足如下条件的程序:
1.分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求:
[1]Point2D有两个整型成员变量x,y(分别为二维空间的X,Y方向坐标),Point2D的构造方法要实现对其成员变量x,y的初始化。
[2]Point2D有一个void型成员方法offset(int a, int b),它可以实现Point2D的平移。
[3]Point3D是Point2D的直接子类,它有有三个整型成员变量x,y,z (分别为三维空间的X,Y,Z方向坐标),Point3D有两个构造方法:Point3D(int x,int y,int z)和Point3D(Point2D p,int z),两者均可实现对Point3D的成员变量x,y,z的初始化。
[4]Point3D有一个void型成员方法offset(int a, int b,int c),该方法可以实现Point3D的平移。
[5]在Point3D中的主函数main()中实例化两个Point2D的对象p2d1,p2d2,打印出它们之间的距离,再实例化两个Point2D的对象p3d1,p3d2,打印出他们之间的距离。
2.定义抽象类Shape,抽象方法为showArea(),求出面积并显示,定义矩形类Rectangle,正方形类Square,圆类 Circle,根据各自的属性,用showArea方法求出各自的面积,在main方法中构造3个对象,调用showArea方法。
3.定义接口DiagArea,其中包含方法double getDiagonal()求对角线长,double getArea()求面积,定义一个矩形类,实现此接口,并自行扩充成员变量和方法,定义一个正方形类继承矩形类(如矩形有长w和宽h,正方形有边x,并有相应的构造函数,有一个方法中一次直接显示边长、面积和对角线长),在另一类中的主方法里使用测试该类。
package shiyan4_1;
class Point2D {
protected int x, y;// 保护类成员主要用于继承
Point2D() {
}
Point2D(int x, int y) {// 有参构造方法
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {// 方便在其他类中使用该类中的私有成员变量
return y;
}
public void offset(int a, int b) {// 偏移后的横纵坐标值
this.x += a;
this.y += b;
}
public void show() {// show横纵坐标
System.out.println("坐标为:[" + x + "," + y + "]");
}
}
class Point3D extends Point2D {// 类的继承
private int z;// 第三点x
Point3D() {
}
Point3D(int x, int y, int z) {// 有参构造方法三点分别初始化
super(2, 3);// 调用父类的有两个参数的构造方法
this.z = z;
}
public int getZ() {
return z;
}
Point3D(Point2D p, int z) {// 有参构造方法,以对象作形参.用p对象调用get方法以初始化继承的私有成员变量
super(p.getX(), p.getY());
this.z = z;
}
public void offset(int a, int b, int c) {// 3D的偏移量
super.offset(a, b);// 方法同上,调用父类的偏移方法
z = z + c;
}
public void show() {// show3D坐标值
System.out.println("坐标为:[" + x + "," + y + "," + z + "]");
}
}
public class shiyan4_1 {
public static void main(String[] args) {
Point2D p2d1 = new Point2D(2, 3);
p2d1.show();
Point2D p2d2 = new Point2D(3, 4);// 初始化2D两点p2d1,p2d2
p2d2.show();
double retDistance2D = distance2D(p2d1, p2d2);
System.out.println("距离为:"+retDistance2D);// 输出2D两点之间距离
Point3D p3d1 = new Point3D(2, 3, 4);
p3d1.show();
Point3D p3d2 = new Point3D(3, 4, 5);
p3d2.show();
double retDistance3D = distance3D(p3d1, p3d2);
System.out.println("距离为:"+retDistance3D);// 同上面的2D
}
public static double distance2D(Point2D p2d1, Point2D p2d2) {// 对象作形参
double distance;
double distanceX;
double distanceY;
distanceX = Math.pow((p2d1.getX() - p2d2.getX()), 2);// (x1-x2)2=X
distanceY = Math.pow((p2d1.getY() - p2d2.getY()), 2);// (y1-y2)2=Y
distance = Math.sqrt(distanceY + distanceX);// X+Y再开方
return distance;
}
public static double distance3D(Point3D p3d1, Point3D p3d2) {// 同2D方法一致
double distance;
double distanceX;
double distanceY;
double distanceZ;
distanceX = Math.pow((p3d1.getX() - p3d2.getX()), 2);
distanceY = Math.pow((p3d1.getY() - p3d2.getY()), 2);
distanceZ = Math.pow((p3d1.getZ() - p3d2.getZ()), 2);
distance = Math.sqrt(distanceY + distanceX + distanceZ);
return distance;
}
}
package shiyan4_2;
abstract class Shape {// 抽象类(一定有抽象方法,但不一定都是抽象方法)
protected double area;
abstract void showArea();// 抽象方法
}
class Rectangle extends Shape {// 类的继承
private double x, y;
Rectangle() {
}
Rectangle(double x, double y) {// 有参构造方法初始化
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void showArea() {// 计算面积并显示.showArea方法的重写
area = getX() * getY();
System.out.println("矩形的长为:"+x);
System.out.println("矩形的宽为:"+y);
System.out.println("矩形的面积为:"+area);
}
}
class Square extends Shape {// 同上
private double x;
Square() {
}
Square(double x) {
this.x = x;
}
public double getX() {
return x;
}
public void showArea() {
area = getX() * getX();
System.out.println("正方形的边长为:"+x);
System.out.println("正方形的面积为:"+area);
}
}
class Circle extends Shape {// 同上
private double r;
Circle() {
}
Circle(double r) {
this.r = r;
}
public double getR() {
return r;
}
public void showArea() {
area = 3.14 * getR() * getR();
System.out.println("圆的半径为:"+r);
System.out.println("圆的面积为:"+area);
}
}
public class shiyan4_2 {
public static void main(String args[]) {
Shape re = new Rectangle(2, 3);
re.showArea();
Shape sq = new Square(2);
sq.showArea();
Shape ci = new Circle(2);
ci.showArea();
}
}
package shiyan4_3;
abstract class Shape {
abstract public void showArea();
}
class Rectangle extends Shape {
protected int weight;
protected int height;
protected int s;
public void showArea() {
this.s = this.weight * this.height;
System.out.println("矩形长为:"+weight);
System.out.println("矩形宽为:"+height);
System.out.println("矩形面积为:"+this.s);
System.out.println("矩形对角线长为:"+Math.sqrt((height*height+weight*weight)));
}
public Rectangle(int weight, int height) {
this.weight = weight;
this.height = height;
}
}
class Square extends Shape {
protected int weight;
protected int s;
public void showArea() {
this.s = this.weight * this.weight;
System.out.println("正方形边长为:"+weight);
System.out.println("正方形面积为:"+this.s);
System.out.println("正方形对角线长为:"+(Math.sqrt(2)*weight));
}
public Square(int weight) {
this.weight = weight;
}
}
interface DiagArea {
public double getDiagonal();
}
public class shiyan4_3 implements DiagArea {
public double getDiagonal() {
return 0.2;
}
public static void main(String[] args) {
Rectangle re = new Rectangle(5, 7);
Square sq = new Square(8);
re.showArea();
sq.showArea();
}
}