HELLO WORLD--一起加油(🍺)!|

kingwzun

园龄:3年6个月粉丝:111关注:0

7-6 sdut-oop-9 计算长方形的周长和面积(类和对象) --python

知识点:
python创建多个构造方法

  1. 使用classmethod
  2. 将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

本文作者:kingwzun

本文链接:https://www.cnblogs.com/kingwz/p/16335457.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   kingwzun  阅读(334)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起