#封装的意义:明确的区分内外,控制外部对隐藏属性的操作
class People:
def __init__(self, name, age):
self.__name = name
self.__age = age
#print('名字是%s年纪是%s'%self.__name,self.__age)
def tell_info(self, name, age):
self.__name = name
self.__age = age
print('名字是%s年纪是%s' %(self.__name, self.__age))
def set_info(self, name, age):
if not isinstance(name, str):
print('必须是字符串')
return
if not isinstance(age, int):
print('必须是数字')
return
self.__name = name
self.__age = age
print('名字是%s年纪是%s' % (self.__name, self.__age))
p = People('laowang', 23)
#p.tell_info('huanggua', 25)
p.set_info('wangba', 58)
print(p.__dict__)
#封装方法的目的 :隔离复杂度
class ATM:
def __card(self):
print('插卡')
def __auth(self):
print('用户认证')
def __input(self):
print('输入金额')
def __print_bill(self):
print('打印账单')
def __take_money(self):
print('取款成功')
def withdrw(self):
self.__card()
self.__auth()
self.__input()
self.__print_bill()
self.__take_money()
a = ATM()
a.withdrw()