面向对象 7 封装之如何实现属性的隐藏&封装的意义&封装可扩展性&property

封装之如何实现属性的隐藏


# class A:
#     __x=1 #'_A__x': 1
#
#     def __init__(self,name):
#         self.__name=name  #self.__A__name=name
#
#     def __foo(self):  #def __A__foo(self)
#         print('run foo')
#
#     def bar(self):
#         self.__foo()  #self._A__foo()
#         print('from bar')
#
# # print(A.__dict__)
# # print(A.__x)
# # print(A.__foo)
#
#
# a=A('egon')
# # a._A__foo()
# # a._A__x
#
# # print(a.__name)  #a.__dict__['__name']
# # print(a.__dict__)
#
# a.bar()

'''
这种变形的特点:
   1、外部无法直接obj.__AttrName
   2、在内部可以直接使用:obj.__AttrName
   3、子类无法覆盖父类__开头的属性
'''

# class Foo:
#     def __func(self): #_Foo__func
#         print('from foo')
#
# class Bar:
#     def __func(self):  #_Bar__func
#         print('from bar')

# b=Bar()
# b.func()

'''
总结 这种变形需要注意的问题:

'''

在这里插入图片描述


class B:
    __x=1

    def __init__(self,name):
        self.__name=name  #self._B__name=name

#验证问题一:
# print(B._B__x)

#验证问题二:
# B.__y=2
# print(B.__dict__)

# b=B('egon')
# print(b.__dict__)
#
# b.__age=18
# print(b.__dict__)
# print(b.__age)

#验证问题三:
class A:
    def __foo(self): #_A__foo
        print('A.foo')

    def bar(self): 
        print('A.foo')
        self.__foo()
class B:
    def __foo(self):
        print('B.foo')

b=B()
b.bar()

封装的意义


#一:封装数据属性:明确区分内外

# class People:
#     def __init__(self,name,age):
#         self.__name=name
#         self.__age=age
#
#     def tell_info(self):
#          print('Name:<%s> Age<%s>'%(self.__name,self.__age))
#
#     def set_info(self,name,age):
#         if not isinstance(name,str):
#             print('名字必须是字符串类型')
#             return
#         if not isinstance(age,int):
#             print('年龄必须是数字类型')
#         self.__name=name
#         self.__age=age
#
# p=People('egon',30)
#
# # p.tell_info()
#
# p.set_info('aa',13)
# p.set_info(1111,'10')
# p.set_info('aa','10')
#
# p.tell_info()

#二、封装方法:隔离复杂度

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 withdraw(self):
        self.__card()
        self.__auth()
        self.__input()
        self.__print_bill()
        self.__take_money()
a=ATM()

a.withdraw()

封装可扩展性


class Room:
    def __init__(self,name,owner,weight,length,height):
        self.name=name
        self.owner=owner

        self.__weight=weight
        self.__length=length
        self.__height=height
    def tell_area(self):
        return self.__weight*self.__length*self.__height

r=Room('卫生间','alex',10,10,10)

print(r.tell_area())

# r.tell_area()

property

'''
bmi 指数
成人bmi

'''

# class People:
#     def __init__(self,name,weight,height):
#         self.name=name
#         self.weight=weight
#         self.height=height
#
#     @property
#     def bmi(self):
#         # print('-------->')
#         return self.weight/(self.height**2)
#
# p=People('egon',75,1.81)
# # p.bmi=p.weight/(p.height**2)
# # print(p.bmi)
#
# print(p.bmi)


class People:
    def __init__(self,name):
        self.__name=name

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self,val):
        # print('setter',val)
        if not isinstance(val,str):
            print('名字必须是字符串类型')
            return
        self.__name=val

    @name.deleter
    def name(self):
        # print('deleter')
        print('不允许删除')


p=People('egon')

# print(p.get_name())

# print(p.name)

# p.name='Egon'
# p.name=123
del p.name
print(p.name)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值