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