小酌一下:只做第一个我,不做第二个谁。
class Rectangle(object):
def __init__(self,width=1,height=2):
self.__width = width
self.__height = height
@property
def width(self):
return self.__width
@width.setter
def width(self,new_width):
self.__width = new_width
#访问器,访问__init__函数中的私有变量
@property
def height(self):
return self.__height
#修改器,可修改私有变量,与上面的访问器对应
@width.setter
def height(self,new_height):
self.__height = new_height
def getArea(self):
area = self.__width * self.__height
print(self.__width,self.__height)
print('面积为%f'%area)
def getPerimeter(self):
round = self.__width + self.__height
print(self.__width,self.__height)
print('周长为%f'%round)
if __name__ == "__main__":
#实列化类
rect = Rectangle(width=4,height=40)
#调用类的函数
rect.getArea()
#在实列化类
rect1 = Rectangle(width=3.5,height=35.7)
rect1.getPerimeter()
class Account(object):
def __init__(self,id_=0,balance=100,annuall=0):
self.__id = id_
self.__balance= balance
self.__annuall=annuall
@property
def id_(self):
return self.__id
@id_.setter
def id(self,new_id):
self.__id = new_id
@property
def balance(self):
return self.__balance
@balance.setter
def balance(self,new_balance):
self.__id = new_balance
@property
def annuall(self):
return self.__annuall
@annuall.setter
def annuall(self,new_annuall):
self.__annuall = new_annuall
def getMonthly(self):
return self.__annuall /12 /100
def getMonthterst(self):
reat = self.getMonthly()
res = self.__balance * reat
return res
def withdrow(self,money):
if self.__balance >= money:
self.__balance -= money
print('你成功取出%.2f'%money)
print('还剩余额为%.2f'%self.__balance)
else:
print('你的余额不足')
print('你的余额为%.2f'%self.__balance)
def deposit(self,money):
self.__balance += money
print('你成功存入%.2f'%money)
print('你的余额为%.2f'%self.__balance)
if __name__ == "__main__":
accent =Account(id_=1122,balance=20000,annuall=4.5)
print(accent.getMonthterst())
print(accent.withdrow(2500))
print(accent.deposit(3000))