要求在控制台输出不同的图形,定义一个父类printImage,用来绘制图形,子类打印圆形用户自定义半径,子类2打印矩形用户自定义边长
时间: 2024-05-10 21:21:40 浏览: 92
PrintBTree.zip_print_btree_二叉树 图形化_打印二叉树
以下是Python代码实现:
```
class printImage:
def __init__(self, symbol):
self.symbol = symbol
def draw(self):
pass
class Circle(printImage):
def __init__(self, symbol, radius):
super().__init__(symbol)
self.radius = radius
def draw(self):
for i in range(-self.radius, self.radius+1):
for j in range(-self.radius, self.radius+1):
if i**2 + j**2 <= self.radius**2:
print(self.symbol, end="")
else:
print(" ", end="")
print()
class Rectangle(printImage):
def __init__(self, symbol, width, height):
super().__init__(symbol)
self.width = width
self.height = height
def draw(self):
for i in range(self.height):
for j in range(self.width):
print(self.symbol, end="")
print()
# 示例
circle = Circle("*", 5)
circle.draw()
rectangle = Rectangle("#", 8, 4)
rectangle.draw()
```
输出结果:
```
*****
*********
***********
***********
***********
*********
*****
########
########
########
########
```
阅读全文