7-4 设计一个矩形类Rectangle
时间: 2023-09-29 16:10:09 浏览: 65
下面是一个简单的矩形类的设计,包含了求面积和周长的方法:
```python
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
```
使用示例:
```python
r = Rectangle(5, 10)
print(r.area()) # 输出 50
print(r.perimeter()) # 输出 30
```