class Account(object): """ 账户 """ owner:str #类型提示 """ 类型提示 """ balnace:float #类型提示 """ 类型提示 """ def __init__(self, owner:any, balance:any): self.owner = owner self.balnace = balance def __repr__(self): """ :return: """ return f'Acount({self.owner!r},{self.balnace!r})' def deposit(self,amount): """ :param amount: :return: """ self.balnace += amount def withdraw(self,amount): """ :param amount: :return: """ self.balnace -= amount def inquiry(self): """ :return: """ return self.balnace #test b=Account("Du",10.0) print(b.inquiry()) b.deposit(50) print(b.inquiry()) b.withdraw(10) print(b.inquiry()) print(b) output: 10.0 60.0 50.0 Acount('Du',50.0)
2.
class Account(object): """ 账户 """ owner: str # 类型提示 """ 类型提示 """ balnace: float # 类型提示 """ 类型提示 """ def __init__(self): self._owner:str = any self._balnace:float = any def __repr__(self): """ :return: """ return f'Acount({self._owner!r},{self._balnace!r})' @property def owner(self): """ :return: """ return self._owner @owner.setter def owner(self,owner): """ :param owner: :return: """ if not isinstance(owner,str): raise TypeError('Exepcted str') if len(owner)>10: raise ValueError('Must be 10 Characters or less') self._owner=owner @property def balnace(self): """ :return: """ return self._balnace @balnace.setter def balnace(self,balnace): """ :param balnace: :return: """ self._balnace=balnace @property def deposit(self): """ :return: """ return self._balnace @deposit.setter def deposit(self, amount): """ :param amount: :return: """ self._balnace += amount @property def withdraw(self): """ :return: """ return self._balnace @withdraw.setter def withdraw(self, amount): """ :param amount: :return: """ self._balnace -= amount @property def inquiry(self): """ :return: """ return self._balnace @owner.deleter def owner(self): """ :return: """ print('deleting') #test b=Account() b.owner='eve' b.balnace=10 print(b.inquiry) b.deposit=50 print(b.inquiry) b.withdraw=10 print(b.inquiry) print(b)
标签:return,python,create,object,._,owner,balnace,self,def From: https://www.cnblogs.com/geovindu/p/18461637