Compare commits
2 Commits
4abb6279cf
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| cd181ec54b | |||
| c623fd1ff3 |
@@ -0,0 +1,3 @@
|
|||||||
|
先定义父类Shape和方法area,并且area要return 0
|
||||||
|
子类Circle和Square要重写area,使用其面积公式
|
||||||
|
主程序先输入side和r,然后列表main存放两个字类,并且使用for循环逐个输出
|
||||||
@@ -14,8 +14,8 @@ class Square(Shape):
|
|||||||
def area(self):
|
def area(self):
|
||||||
return self.side * self.side
|
return self.side * self.side
|
||||||
|
|
||||||
r = int(input("请输入圆的半径: "))
|
r = float(input("请输入圆的半径: "))
|
||||||
side = int(input("请输入正方形的边长: "))
|
side = float(input("请输入正方形的边长: "))
|
||||||
shapes = [Circle(r), Square(side)]
|
shapes = [Circle(r), Square(side)]
|
||||||
for i in shapes:
|
for i in shapes:
|
||||||
print(i.__class__.__name__ + "的面积为: " + str(i.area()))
|
print(i.__class__.__name__ + "的面积为: " + str(i.area()))
|
||||||
Reference in New Issue
Block a user