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.

42 lines
1019 B

package tiaozhanti;
import java.util.ArrayList;
import java.util.List;
/**
* 复合图形(可包含多个子图形)
*/
public class CompositeShape implements Shape {
private String color; // 复合图形整体颜色
private List<Shape> children = new ArrayList<>(); // 子图形列表
public CompositeShape(String color) {
this.color = color;
}
// 添加子图形
public void addShape(Shape shape) {
if (shape != null) children.add(shape);
}
// 移除子图形
public void removeShape(Shape shape) {
children.remove(shape);
}
@Override
public String getColor() {
return color;
}
@Override
public double getArea() {
// 总面积=所有子图形面积之和
return children.stream().mapToDouble(Shape::getArea).sum();
}
// 获取子图形列表(测试用)
public List<Shape> getChildren() {
return new ArrayList<>(children);
}
}