a.设计一个 NewCircle 类,继承 Circle 类,并添加新的属性颜色 color, 重写 get_info 方法,以输出该新对象的相关信息,例如输出信息为:这是 NewCircle 类的一个对象,半径为 r,颜色为 color(其中 r 和 color 输出当前对象的实 际值)
时间: 2024-02-22 22:57:53 浏览: 24
好的,以下是 Python 代码实现:
```python
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Circle:
def __init__(self, center, radius):
self.center = center
self.radius = radius
def get_info(self):
return "这是 Circle 类的一个对象,半径为 {},中心坐标为 ({}, {})".format(self.radius, self.center.x, self.center.y)
class NewCircle(Circle):
def __init__(self, center, radius, color):
super().__init__(center, radius)
self.color = color
def get_info(self):
return "这是 NewCircle 类的一个对象,半径为 {},中心坐标为 ({}, {}),颜色为 {}".format(self.radius, self.center.x, self.center.y, self.color)
```
使用示例:
```python
center = Point(0, 0)
circle = Circle(center, 5)
print(circle.get_info()) # 输出:这是 Circle 类的一个对象,半径为 5,中心坐标为 (0, 0)
new_circle = NewCircle(center, 5, "red")
print(new_circle.get_info()) # 输出:这是 NewCircle 类的一个对象,半径为 5,中心坐标为 (0, 0),颜色为 red
```