定义一个点类,包含两个属性:横坐标和纵坐标。通过继承点类再设计一个圆类,新增私有属性:半径、周长、面积,新增方法:设置圆 的半径、获取圆的半径、计算圆的周长、获取圆的周长、计算圆的面积,获取圆的面积。设计一个测试类,把坐标、半径传递后,显示圆的 标、 27 半径、周长和面积。
时间: 2023-07-10 12:06:47 浏览: 77
python实现可暂停的动态曲线绘制,纵坐标固定,横坐标从上一个点到当前点,同时保存数据到文件中
点类的定义如下:
```java
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
```
圆类的定义如下:
```java
public class Circle extends Point {
private double radius;
private double circumference;
private double area;
public Circle(double x, double y) {
super(x, y);
}
public void setRadius(double radius) {
this.radius = radius;
calculateCircumference();
calculateArea();
}
public double getRadius() {
return radius;
}
public double getCircumference() {
return circumference;
}
public double getArea() {
return area;
}
private void calculateCircumference() {
circumference = 2 * Math.PI * radius;
}
private void calculateArea() {
area = Math.PI * radius * radius;
}
}
```
测试类的定义如下:
```java
public class CircleTest {
public static void main(String[] args) {
Circle circle = new Circle(3, 4);
circle.setRadius(5);
System.out.println("圆心坐标: (" + circle.getX() + ", " + circle.getY() + ")");
System.out.println("半径: " + circle.getRadius());
System.out.println("周长: " + circle.getCircumference());
System.out.println("面积: " + circle.getArea());
}
}
```
输出结果为:
```
圆心坐标: (3.0, 4.0)
半径: 5.0
周长: 31.41592653589793
面积: 78.53981633974483
```
阅读全文