写一个circle类辅助类包括私有的成员变量、各种重载的构造方法、用setter方法和getter方法以及tostring方法 测试的主类
时间: 2023-05-30 07:02:02 浏览: 151
Circle类:
```java
public class Circle {
private double radius;
private String color;
public Circle() {
this.radius = 1.0;
this.color = "red";
}
public Circle(double radius) {
this.radius = radius;
this.color = "red";
}
public Circle(double radius, String color) {
this.radius = radius;
this.color = color;
}
public double getRadius() {
return this.radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
public double getArea() {
return Math.PI * this.radius * this.radius;
}
public String toString() {
return "Circle[radius=" + this.radius + ",color=" + this.color + "]";
}
}
```
测试主类:
```java
public class TestCircle {
public static void main(String[] args) {
Circle c1 = new Circle();
System.out.println("The circle has radius of " + c1.getRadius() + " and area of " + c1.getArea());
Circle c2 = new Circle(2.0);
System.out.println("The circle has radius of " + c2.getRadius() + " and area of " + c2.getArea());
Circle c3 = new Circle(3.0, "blue");
System.out.println("The circle has radius of " + c3.getRadius() + " and area of " + c3.getArea());
c3.setColor("green");
c3.setRadius(4.0);
System.out.println("The circle has radius of " + c3.getRadius() + " and area of " + c3.getArea());
System.out.println(c3.toString());
}
}
```
输出结果:
```
The circle has radius of 1.0 and area of 3.141592653589793
The circle has radius of 2.0 and area of 12.566370614359172
The circle has radius of 3.0 and area of 28.274333882308138
The circle has radius of 4.0 and area of 50.26548245743669
Circle[radius=4.0,color=green]
```
阅读全文