本帖最后由 jackz007 于 2022-11-16 14:16 编辑 class LandW:
def __init__(self , length = None , width = None) :
if length and width and length > 0 and width > 0 :
self . l , self . w = length , width
else :
self . l , self . w = None , None
self . setLandW()
def setLandW(self , length = None , width = None) :
if length and width and length > 0 and width > 0 :
self . l , self . w = length , width
else :
while True :
length , width = map(eval , input("请输入矩形的长和宽 (长 宽) :") . split())
if length > 0 and width > 0 : self . l , self . w = length , width
if self . l and self . w and self . l > 0 and self . w > 0 : break
def getLandW(self):
print("矩形的长为:{},宽为:{}" . format(self.l , self.w))
def getArea(self):
print("矩形的面积为:{}" . format(self . l * self . w))
def help_lw(self):
print("l . setLandW() -- 设置矩形的长和宽")
print("l . getLandW() -- 检查您所输入矩形的长和宽")
print("l . getArea() -- 查看您所输入的矩形的面积")
def __repr__(self):
return "矩形的面积为:{}" . format(self . l * self . w)
__str__ = __repr__
l = LandW()
l . getArea()
x = LandW(13 , 27)
print(x)
x . setLandW(3 , 7)
print(x)
运行实况:D:\[00.Exerciese.2022]\Python>python x.py
请输入矩形的长和宽 (长 宽) :3 5
矩形的面积为:15
矩形的面积为:351
矩形的面积为:21
D:\[00.Exerciese.2022]\Python>
|