【Python Practice】Day 13 Question 47-50
程序员文章站
2022-04-16 16:16:49
'''@Author: your name@Date: 2020-07-19 10:29:31@LastEditTime: 2020-07-19 10:51:19@LastEditors: Please set LastEditors@Description: In User Settings Edit@FilePath: \vscode_py\day13.py'''import math# Question 47# 写一个类,计算圆的面积class Circle(): def...
'''
@Author: your name
@Date: 2020-07-19 10:29:31
@LastEditTime: 2020-07-19 10:51:19
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: \vscode_py\day13.py
'''
import math
# Question 47
# 写一个类,计算圆的面积
class Circle():
def __init__(self,r):
self.r = r
print("Circle constructor")
def area(self):
self.area=(self.r)**2*math.pi
# Question 48
# 矩形类
class Rect():
def __init__(self,l,w):
self.l = l
self.w = w
print("Rect constructor")
def area(self):
self.area=(self.l*self.w)
print("Rect area:{}".format(self.area))
# Question 49
# 定义一个类Shape,其子类为Square,计算面积
class Shape():
def __init__(self):
pass
def area(self):
return 0
class Square(Shape):
def __init__(self,length=0):
Shape.__init__(self) # 父类初始化
self.length = length
# self.area=0
print("Square constructor")
def area(self):
self.area=(self.length)**2
print("Square:{}".format(self.area))
# Question 50
# Please raise a RuntimeError exception.
# Use raise() to raise an exception.
def Q50():
raise RuntimeError("something wrong")
if __name__ == "__main__":
# c=Circle(10)
# c.area()
# print(c.area)
# r=Rect(10,5)
# r.area()
# s=Square(3)
# s.area()
# f=Shape()
# print(f.area())
# s1=Square()
# s1.area()
Q50()
本文地址:https://blog.csdn.net/suyunzzz/article/details/107442081
上一篇: MATLAB基础