定义一个是shape抽象类,利用他作为基类派生出Retangle,circle等具体形状类,已知,具体形状类均具有两个方法Getarea,Getcolor,分别用来得到形状的面积和颜色,最后编写一个测试程序对产生的类的功能进行验证
时间: 2023-09-20 19:05:46 浏览: 121
### 回答1:
下面是Python的实现:
```python
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def get_area(self):
pass
@abstractmethod
def get_color(self):
pass
class Rectangle(Shape):
def __init__(self, width, height, color):
self.width = width
self.height = height
self.color = color
def get_area(self):
return self.width * self.height
def get_color(self):
return self.color
class Circle(Shape):
def __init__(self, radius, color):
self.radius = radius
self.color = color
def get_area(self):
return 3.14 * self.radius ** 2
def get_color(self):
return self.color
# 测试程序
r = Rectangle(10, 5, "blue")
c = Circle(3, "red")
print("Rectangle's area:", r.get_area())
print("Rectangle's color:", r.get_color())
print("Circle's area:", c.get_area())
print("Circle's color:", c.get_color())
```
输出结果:
```
Rectangle's area: 50
Rectangle's color: blue
Circle's area: 28.26
Circle's color: red
```
### 回答2:
以下是一个符合要求的示例代码:
```python
# 定义一个抽象类Shape
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def get_area(self):
pass
@abstractmethod
def get_color(self):
pass
# 继承Shape类,定义具体形状类Rectangle
class Rectangle(Shape):
def __init__(self, width, height, color):
self.width = width
self.height = height
self.color = color
def get_area(self):
return self.width * self.height
def get_color(self):
return self.color
# 继承Shape类,定义具体形状类Circle
class Circle(Shape):
def __init__(self, radius, color):
self.radius = radius
self.color = color
def get_area(self):
return 3.14 * self.radius ** 2
def get_color(self):
return self.color
# 测试程序
rectangle = Rectangle(4, 6, "red")
print("矩形的面积:", rectangle.get_area())
print("矩形的颜色:", rectangle.get_color())
circle = Circle(3, "blue")
print("圆形的面积:", circle.get_area())
print("圆形的颜色:", circle.get_color())
```
运行结果如下:
```
矩形的面积: 24
矩形的颜色: red
圆形的面积: 28.26
圆形的颜色: blue
```
### 回答3:
定义一个抽象类Shape作为基类,可以派生出Rectangle、Circle等具体形状类。在具体形状类中,都有两个方法GetArea和GetColor,分别用来获取形状的面积和颜色。最后编写一个测试程序来验证这些类的功能。
首先,我们定义一个抽象类Shape作为基类:
```python
abstract class Shape:
def GetArea(self):
pass
def GetColor(self):
pass
```
接下来,我们派生出具体形状类Rectangle和Circle。在这两个类中,我们需要实现基类Shape中定义的两个抽象方法:
```python
class Rectangle(Shape):
def __init__(self, width, height, color):
self.width = width
self.height = height
self.color = color
def GetArea(self):
return self.width * self.height
def GetColor(self):
return self.color
class Circle(Shape):
def __init__(self, radius, color):
self.radius = radius
self.color = color
def GetArea(self):
return 3.14 * self.radius * self.radius
def GetColor(self):
return self.color
```
最后,我们编写一个测试程序来验证这些类的功能:
```python
# 创建一个Rectangle对象,并获取它的面积和颜色
rectangle = Rectangle(5, 3, "红色")
area = rectangle.GetArea()
color = rectangle.GetColor()
print("Rectangle的面积为:", area)
print("Rectangle的颜色为:", color)
# 创建一个Circle对象,并获取它的面积和颜色
circle = Circle(2, "蓝色")
area = circle.GetArea()
color = circle.GetColor()
print("Circle的面积为:", area)
print("Circle的颜色为:", color)
```
输出结果:
```
Rectangle的面积为: 15
Rectangle的颜色为: 红色
Circle的面积为: 12.56
Circle的颜色为: 蓝色
```
通过这个测试程序,我们验证了定义的抽象类Shape和具体形状类Rectangle、Circle的功能,成功获取了它们的面积和颜色信息。
阅读全文