码迷,mamicode.com
首页 > 其他好文 > 详细

封装的意义

时间:2019-01-11 11:28:38      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:封装   输入   ict   inpu   账单   区分   owa   color   pre   

#封装的意义:明确的区分内外,控制外部对隐藏属性的操作
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()

封装的意义

标签:封装   输入   ict   inpu   账单   区分   owa   color   pre   

原文地址:https://www.cnblogs.com/yuexijun/p/10253864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!