设计圆形类 Circle,继承接口 Shape;. Shape 中有 getArea()方法;. I Circle 中实现圆的面积。.
时间: 2024-11-07 09:12:56 浏览: 28
设计一个名为`Circle`的类,它继承自抽象接口`Shape`。`Shape`接口通常包含一些通用的操作或属性,如获取面积的方法`getArea()`。在这个场景下,`Circle`类会覆盖`Shape`接口中的`getArea()`方法,用于计算并返回圆的具体面积。
```java
// 定义Shape接口
public interface Shape {
double getArea();
}
// 实现Circle类,继承Shape接口
public class Circle implements Shape {
private double radius;
// 构造函数
public Circle(double radius) {
this.radius = radius;
}
// 实现getArea()方法,计算圆的面积
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
```
在上述代码中,当你创建`Circle`对象并调用其`getArea()`方法时,实际上是通过`Shape`接口调用的,但实际计算的是`Circle`类中的面积公式。这就是多态的应用,即同一个方法名可以根据实例的不同而有不同的实现。
相关问题
定义一个形状类Shape,提供计算周长getPerimeter()和面积getArea()的函数。 定义一个子类正方形类Square继承自Shape类,拥有边长属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()。 定义一个子类长方形类Rectangle继承自Square类,拥有长、宽属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()。 定义一个子类圆形类Circle继承自Shape,拥有半径属性,提供构造函数,能够计算周长getPerimeter()和面积getArea()。 设计一个show函数,对Shape类的变量,输出周长、面积。
好的,以下是代码实现:
```python
class Shape:
def __init__(self):
pass
def getPerimeter(self):
pass
def getArea(self):
pass
def show(self):
print("周长:", self.getPerimeter())
print("面积:", self.getArea())
class Square(Shape):
def __init__(self, side):
self.side = side
def getPerimeter(self):
return 4 * self.side
def getArea(self):
return self.side ** 2
class Rectangle(Square):
def __init__(self, length, width):
super().__init__(length)
self.width = width
def getPerimeter(self):
return 2 * (self.width + self.side)
def getArea(self):
return self.side * self.width
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def getPerimeter(self):
return 2 * 3.14 * self.radius
def getArea(self):
return 3.14 * (self.radius ** 2)
```
测试代码如下:
```python
s = Square(5)
s.show()
r = Rectangle(4, 6)
r.show()
c = Circle(3)
c.show()
```
输出结果如下:
```
周长: 20
面积: 25
周长: 20
面积: 24
周长: 18.84
面积: 28.26
```
写出代码// 定义抽象类 Shape 表示几何图形有哪些共同的属性和方法 public abstract class Shape { protected int x, y; public Shape(int x, int y) { this.x = x; this.y = y; } public abstract double getArea(); // 抽象方法,由子类具体实现 public abstract double getPerimeter(); // 抽象方法,由子类具体实现 public void move(int dx, int dy) { this.x += dx; this.y += dy; } } // 定义矩形 Rectangle 类继承自 Shape 类,并重写 Shape 中的抽象方法 public class Rectangle extends Shape { private int width, height; public Rectangle(int width, int height, int x, int y) { super(x, y); this.width = width; this.height = height; } @Override public double getArea() { return width * height; } @Override public double getPerimeter() { return 2 * (width + height); } } // 定义圆形 Circle 类继承自 Shape 类,并重写 Shape 中的抽象方法 public class Circle extends Shape { private int radius; public Circle(int radius, int x, int y) { super(x, y); this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public double getPerimeter() { return 2 * Math.PI * radius; } } // 使用多态性,创建一个 Shape 对象的列表,包含 Rectangle 和 Circle 对象 public class Main { public static void main(String[] args) { Shape[] shapes = {new Rectangle(3, 4, 0, 0), new Circle(5, 0, 0)}; for (Shape shape : shapes) { // 调用通用的方法,因为 Rectangle 和 Circle 都是 Shape 类的子类 System.out.printf("Area: %.2f, Perimeter: %.2f\n", shape.getArea(), shape.getPerimeter()); // 调用和重写抽象类中的方法会自动调用对应子类的方法 shape.move(1, 1); System.out.printf("New position: (%d, %d)\n", shape.x, shape.y); } } }的实验结果分析
这段代码定义了一个抽象类 Shape,表示几何图形具有哪些共同的属性和方法,其中包括构造方法、抽象方法 getArea 和 getPerimeter,以及方法 move。然后定义了矩形 Rectangle 类和圆形 Circle 类分别继承自 Shape 类,并重写了 Shape 中的抽象方法。
在 Main 类中,使用多态性创建了一个 Shape 对象的列表,包含 Rectangle 和 Circle 对象,然后遍历这个列表,调用通用的方法输出每个对象的面积和周长,并调用 move 方法移动对象的位置,并输出新的位置。
运行该程序,输出结果如下:
Area: 12.00, Perimeter: 14.00
New position: (1, 1)
Area: 78.54, Perimeter: 31.42
New position: (1, 1)
可以看出,程序成功创建了包含 Rectangle 和 Circle 对象的 Shape 对象列表,并正确计算了每个对象的面积和周长,并成功移动了对象的位置,输出了新的位置。这说明程序成功实现了多态性和继承的概念。
阅读全文
相关推荐















