设计 Shape、Square、Circle3个类,计算周长和面积,编写 ShapeTest 类创建子类对象和输出其周长、面积。数据转换的事件处理
时间: 2024-01-30 08:02:26 浏览: 62
c++—用类进行圆的周长和面积的计算
好的,下面是对应的代码实现:
```java
// Shape类
public abstract class Shape {
public abstract double getArea();
public abstract double getPerimeter();
}
// Square类
public class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
@Override
public double getArea() {
return side * side;
}
@Override
public double getPerimeter() {
return 4 * side;
}
}
// Circle类
public class Circle extends Shape {
private double radius;
private final double PI = 3.14159265358979323846;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * PI * radius;
}
}
// ShapeTest类
public class ShapeTest {
public static void main(String[] args) {
Shape square = new Square(5);
Shape circle = new Circle(3);
System.out.println("Square area: " + square.getArea());
System.out.println("Square perimeter: " + square.getPerimeter());
System.out.println("Circle area: " + circle.getArea());
System.out.println("Circle perimeter: " + circle.getPerimeter());
}
}
```
关于数据转换的事件处理,我们可以在父类 Shape 中添加一些方法,例如将 Shape 转换为字符串表示,或者将字符串表示转换为 Shape 对象。在这里,我们可以考虑使用 JSON 格式,例如将 Square 对象转换为以下 JSON 字符串表示:
```json
{
"type": "square",
"side": 5
}
```
这样的话,我们就可以使用一些 JSON 库来实现 Shape 对象和 JSON 字符串之间的转换。具体实现可以参考以下代码:
```java
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
// Shape类
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Square.class, name = "square"),
@JsonSubTypes.Type(value = Circle.class, name = "circle")
})
public abstract class Shape {
public abstract double getArea();
public abstract double getPerimeter();
public String toJson() throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(this);
}
public static Shape fromJson(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, Shape.class);
}
}
// Square类
public class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
@Override
public double getArea() {
return side * side;
}
@Override
public double getPerimeter() {
return 4 * side;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
}
// Circle类
public class Circle extends Shape {
private double radius;
private final double PI = 3.14159265358979323846;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * PI * radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
// ShapeTest类
public class ShapeTest {
public static void main(String[] args) throws Exception {
Shape square = new Square(5);
Shape circle = new Circle(3);
String squareJson = square.toJson();
String circleJson = circle.toJson();
Shape square2 = Shape.fromJson(squareJson);
Shape circle2 = Shape.fromJson(circleJson);
System.out.println(square2.getArea());
System.out.println(circle2.getPerimeter());
}
}
```
这里我们使用了 Jackson 库来实现 Shape 对象和 JSON 字符串之间的转换,同时为了实现多态,我们使用了 Jackson 中的 @JsonTypeInfo 和 @JsonSubTypes 注解。这样就可以实现 Shape 对象的序列化和反序列化,以及多态的处理。
阅读全文