7-6 sdut-oop-9 计算长方形的周长和面积(类和对象) --python
知识点:
python创建多个构造方法
- 使用classmethod
- 将init的参数设为可变类型,在init语句中判断
class Rect:
__length = 0
__width = 0
def __init__(self, l, w):
self.__length = l
self.__width = w
@classmethod
def initsec(self, l):
return self(l, l)
def leng(self):
return self.__length
def widt(self):
return self.__width
def e(self):
return self.__width * self.__length
def z(self):
return 2 * (self.__width + self.__length)
while True:
try:
num = [int(x) for x in input().split()]
if len(num) == 1:
if num[0] <= 0:
num[0] = 0
t = Rect.initsec(num[0])
else:
if num[0] <= 0 or num[1] <= 0:
num[0] = 0
num[1] = 0
t = Rect(num[0], num[1])
print(t.leng(), t.widt(), t.z(), t.e())
except:
break