定义并实现一个矩形类Rectangle,其私有实例成员为矩形的左下角与右上角两个点的坐标,能设置左下角和右上角两个点的位置,能根据左下角与右上角两个点的坐标计算矩形的长、宽、周长和面积,另外根据需要适当添加其他成员方法和特殊方法(如构造方法)。实现并测试这个类。
class Rectangle:
def __init__(self,x1,x2,y1,y2):
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
def change(self):
check1=eval(input('输入分别左下右上的四个参数'))
check2=eval(input())
check3=eval(input())
check4=eval(input())
if check1>= check2 or check3>=check4: #判断用户是否输入的左上,右上两个点的值
print('请重新输入')
else:
self.x1=check1
self.x2=check2
self.y1=check3
self.y2=check4
def outputL(self):
return self.x2-self.x1
def outputD(self):
return self.y2-self.y1
def outputC(self):
return (self.outputL()+self.outputD())*2
def outputS(self):
return (self.outputL()*self.outputD())
def main1(self):#主菜单
user21=Rectangle(1,2,3,4)
for i in range(0,10000):
choice=eval(input('1.输入参数,2.输出长,3.输出宽,.4.输出周长,5.输出面积'))
if choice == 1:
user21.change()
if choice == 2:
print(user21.outputL())
if choice == 3:
print(user21.outputD())
if choice == 4:
print(user21.outputC())
if choice == 5:
print(user21.outputS())
else:
continue
import time
class Rectangle:
def __init__(self,x1,x2,y1,y2):
self.x1=x1
self.x2=x2
self.y1=y1
self.y2=y2
def change(self):
check1=eval(input('输入分别左下右上的四个参数'))
check2=eval(input())
check3=eval(input())
check4=eval(input())
if check1>= check2 or check3>=check4:
print('请重新输入')
else:
self.x1=check1
self.x2=check2
self.y1=check3
self.y2=check4
def outputL(self):
return self.x2-self.x1
def outputD(self):
return self.y2-self.y1
def outputC(self):
return (self.outputL()+self.outputD())*2
def outputS(self):
return (self.outputL()*self.outputD())
def main1(self):#主菜单
user21=Rectangle(1,2,3,4)
for i in range(0,10000):
choice=eval(input('1.输入参数,2.输出长,3.输出宽,.4.输出周长,5.输出面积'))
if choice == 1:
user21.change()
if choice == 2:
print(user21.outputL())
if choice == 3:
print(user21.outputD())
if choice == 4:
print(user21.outputC())
if choice == 5:
print(user21.outputS())
else:
continue
#main1(1)#?调用RECTANGLE