You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package challenge;
import basic.Shape;
import advanced.Resizable;
/**
* 圆形类实现Shape接口和Resizable接口
*/
public class Circle implements Shape, Resizable {
private String color;
private double radius;
/**
* 构造方法
* @param color 圆形的颜色
* @param radius 圆形的半径
*/
public Circle(String color, double radius) {
this.color = color;
this.radius = radius;
}
@Override
public String getColor() {
return color;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public void display() {
System.out.println("Circle [color=" + color + ", radius=" + radius + ", area=" + getArea() + "]");
}
@Override
public void enlarge(double factor) {
if (factor > 0) {
this.radius *= factor;
}
}
@Override
public void shrink(double factor) {
if (factor > 0 && factor < 1) {
this.radius *= factor;
}
}
@Override
public String getSizeInfo() {
return "Circle with radius: " + radius + ", area: " + getArea();
}
/**
* 获取半径
* @return 半径
*/
public double getRadius() {
return radius;
}
/**
* 设置半径
* @param radius 新的半径
*/
public void setRadius(double radius) {
this.radius = radius;
}
/**
* 设置颜色
* @param color 新的颜色
*/
public void setColor(String color) {
this.color = color;
}
}