• 封装的意义


    #封装的意义:明确的区分内外,控制外部对隐藏属性的操作
    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()
  • 相关阅读:
    弦图点染色问题
    BZOJ1098: [POI2007]办公楼biu
    BZOJ1097: [POI2007]旅游景点atr
    BZOJ1068: [SCOI2007]压缩
    BZOJ1055: [HAOI2008]玩具取名
    BZOJ4199: [Noi2015]品酒大会
    BZOJ2527: [Poi2011]Meteors
    BZOJ1493 [NOI2007]项链工厂
    BZOJ1095 ZJOI2007 Hide 捉迷藏
    bzoj1468 Tree
  • 原文地址:https://www.cnblogs.com/yuexijun/p/10253864.html
Copyright © 2020-2023  润新知