class Rectangle extends Shape
{
private double length;
private double width;
public Rectangle(String color, double length, double width)
{
super(color);
this.length = length;
this.width = width;
}
public double getArea()
{
return length * width;
}
}
public class Main {
public static void main(String[] args)
{
Circle circle = new Circle("红色", 5.0);
Rectangle rectangle = new Rectangle("蓝色", 4.0, 3.0);
circle.draw(); // 输出: 绘制一个红色的形状
System.out.println("圆的面积:" + circle.getArea()); // 输出: 圆的面积:78.53981633974483
rectangle.draw(); // 输出: 绘制一个蓝色的形状
System.out.println("矩形的面积:" + rectangle.getArea()); // 输出: 矩形的面积:12.0
}
}
java继承
最新推荐文章于 2024-08-23 15:54:50 发布