实现Point2D和Point3D,并在main方法中创建Point3D对象
时间: 2023-12-28 07:05:13 浏览: 75
好的,我可以帮你实现这个功能。以下是代码示例:
```java
// Point2D类
public class Point2D {
private double x;
private double y;
public Point2D(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
}
// Point3D类
public class Point3D extends Point2D {
private double z;
public Point3D(double x, double y, double z) {
super(x, y);
this.z = z;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
}
// main方法
public class Main {
public static void main(String[] args) {
Point3D point3D = new Point3D(1.0, 2.0, 3.0);
System.out.println("Point3D(x=" + point3D.getX()
+ ", y=" + point3D.getY() + ", z=" + point3D.getZ() + ")");
}
}
```
在上面的代码中,我们定义了 `Point2D` 类,表示二维点,包含 `x` 和 `y` 坐标属性。我们还定义了 `Point3D` 类,它继承自 `Point2D` 类,表示三维点,增加了 `z` 坐标属性。在 `main` 方法中,我们创建了一个 `Point3D` 对象,并打印了它的坐标信息。
输出结果为:
```
Point3D(x=1.0, y=2.0, z=3.0)
```
阅读全文