用java分别编写两个类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,打印出它们之间的距离,再实例化两个Point3D的对象p3d1,p3d2,打印出他们之间的距离。
时间: 2023-12-21 07:03:30 浏览: 36
好的,以下是Java版本的代码实现:
```java
import java.lang.Math;
class Point2D {
int x;
int y;
public Point2D(int x, int y) {
this.x = x;
this.y = y;
}
public void offset(int a, int b) {
this.x += a;
this.y += b;
}
}
class Point3D extends Point2D {
int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public Point3D(Point2D p, int z) {
super(p.x, p.y);
this.z = z;
}
public void offset(int a, int b, int c) {
super.offset(a, b);
this.z += c;
}
}
public class Main {
public static void main(String[] args) {
Point2D p2d1 = new Point2D(0, 0);
Point2D p2d2 = new Point2D(3, 4);
System.out.println(distance(p2d1, p2d2));
Point3D p3d1 = new Point3D(0, 0, 0);
Point3D p3d2 = new Point3D(p2d2, 5);
System.out.println(distance(p3d1, p3d2));
}
public static double distance(Point2D p1, Point2D p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
public static double distance(Point3D p1, Point3D p2) {
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2) + Math.pow(p1.z - p2.z, 2));
}
}
```
输出:
```
5.0
6.708203932499369
```
其中,`distance`方法可以计算任意两个点之间的距离,无论是二维还是三维。