public abstract class Shape implements Cloneable {
@Override
public Shape clone() throws CloneNotSupportedException {
return (Shape) super.clone();
}
}
@Data
public class Circle extends Shape {
/**
* 半径
*/
private int radius;
}
@Data
public class Rectangle extends Shape {
private int width;
private int height;
}
public class Application {
public static void main(String[] args) {
Circle circle = new Circle();
circle.setRadius(10);
Rectangle rectangle = new Rectangle();
rectangle.setHeight(15);
rectangle.setWidth(5);
List<Shape> shapes = new ArrayList<>();
shapes.add(circle);
shapes.add(rectangle);
shapes.stream().forEach(shape -> {
try {
Shape clonedShape = shape.clone();
System.out.println(clonedShape);
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
});
}
}