- 分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求:
-
Point2D有两个整型成员变量x, y (分别为二维空间的X,Y方向坐标),Point2D的构造方法要实现对其成员变量x, y的初始化。
-
Point2D有一个void型成员方法offset(int a, int b),它可以实现Point2D的平移。
-
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的初始化。 -
Point3D有一个void型成员方法offset(int a, int b, int c),该方法可以实现Point3D的平移。
-
在Point3D中的主函数main()中实例化两个Point2D的对象p2d1,p2d2,打印出它们之间的距离,再实例化两个Point2D的对象p3d1,p3d2,打印出他们之间的距离。
package test;
public class Point2D {
int x;
int y;
Point2D(int _x,int _y){
x=_x;
y=_y;
}
public void offset(int a,int b) {
x+=a;
y+=b;
}
}
package test;
public class Point3D extends Point2D{
int z;
Point3D(int _x, int _y,int _z) {
super(_x, _y);
z=_z;
}
Point3D(Point2D p,int _z){
super(p.x,p.y);
z=_z;
}
public void offset(int a,int b,int c) {
x+=a;
y+=b;
z+=c;
}
public static void main(String[] args) {
Point2D p2d1=new Point2D(1,4);
Point2D p2d2=new Point2D(2,5);
System.out.println(Math.sqrt(Math.pow((p2d1.x-p2d2.x),2)+Math.pow((p2d1.y-p2d2.y),2)));
Point3D p3d1=new Point3D(1,4,8);
Point3D p3d2=new Point3D(2,5,9);
System.out.println(Math.sqrt(Math.pow((p3d1.x-p3d2.x),2)+Math.pow((p3d1.y-p3d2.y),2)+Math.pow((p3d1.z-p3d2.z),2)));
}
}