3、定义一个 shape 抽象类,派生出 Rectangle 类和 Circle 类,计算各派生类对象的面积 Area()。
时间: 2023-06-19 19:09:55 浏览: 70
以下是 Python 代码实现:
```python
from abc import ABC, abstractmethod
import math
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
```
这段代码定义了一个抽象类 Shape,其中有一个抽象方法 area(),派生出了 Rectangle 和 Circle 两个具体类,并实现了它们各自的 area() 方法来计算面积。其中,Rectangle 的面积计算公式为长乘宽,Circle 的面积计算公式为 πr²。
相关问题
写一个程序,定义抽象基类Shape,由它派生出3个派生类:circle圆形 rectangle矩形
好的,很高兴为您解答这个问题。以下是一个使用Python语言实现的示例程序:
```python
from abc import ABC, abstractmethod
import math
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
def perimeter(self):
return 2 * math.pi * self.radius
class Rectangle(Shape):
def __init__(self, height, width):
self.height = height
self.width = width
def area(self):
return self.height * self.width
def perimeter(self):
return 2 * (self.height + self.width)
# Example usage
circle = Circle(5)
print(circle.area()) # Output: 78.53981633974483
print(circle.perimeter()) # Output: 31.41592653589793
rectangle = Rectangle(4, 6)
print(rectangle.area()) # Output: 24
print(rectangle.perimeter()) # Output: 20
```
在这个示例程序中,我们首先定义了一个抽象基类Shape,它包含两个抽象方法area和perimeter。接着,我们定义了两个派生类Circle和Rectangle,它们都继承自Shape基类,并实现了area和perimeter方法。Circle类接收一个半径参数,并使用math库来计算面积和周长。Rectangle类接收一个高度和宽度参数,并使用简单的公式来计算面积和周长。
最后,我们通过创建Circle和Rectangle类的实例,并调用它们的area和perimeter方法来测试代码的正确性。
定义一个抽象类Shape,它包含一个抽象方法getArea(),从Shape类派生出Rectangle和Circle类,这两个类都用getArea()方法计算对象的面积。编写应用程序使用Rectange和Circle类
好的,下面是对应的代码:
```python
# 定义抽象类 Shape
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def getArea(self):
pass
# 定义 Rectangle 类
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def getArea(self):
return self.length * self.width
# 定义 Circle 类
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def getArea(self):
return 3.14 * self.radius ** 2
# 应用程序
if __name__ == '__main__':
rectangle = Rectangle(5, 10)
circle = Circle(7)
print('Rectangle area:', rectangle.getArea())
print('Circle area:', circle.getArea())
```
在这个例子中,我们定义了一个抽象类 Shape,它包含一个抽象方法 getArea()。然后我们从 Shape 类派生出 Rectangle 和 Circle 类,并且这两个类都实现了 getArea() 方法来计算对象的面积。
在应用程序中,我们创建了一个 Rectangle 对象和一个 Circle 对象,并且调用它们的 getArea() 方法来计算它们的面积。最后,我们打印出这两个图形的面积。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)