类的封装

时间:2019-06-08 00:22:20   收藏:0   阅读:182

什么是封装

如何封装

class People:
    __country = 'China'  # _People__country = 'China'
    __n = 100  # _People__n = 100

    def __init__(self, name, age, sex):
        self.__name = name  # self._People__name = name
        self.age = age
        self.sex = sex

    def eat(self):
        print('eat.....')


peo1 = People('egon', 18, 'male')

技术分享图片

print(People.__dict__)
{'__module__': '__main__', '_People__country': 'China', '_People__n': 100, '__init__': <function People.__init__ at 0x10953af28>, 'eat': <function People.eat at 0x10953aea0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None}
try:
    print(peo1.__name)
except Exception as e:
    print(e)
'People' object has no attribute '__name'
try:
    print(peo1.__country)
except Exception as e:
    print(e)
'People' object has no attribute '__country'

1.虽然类隐藏的属性外部无法直接访问,但是可以通过_类名__属性名访问,但是这样做明天你就可以走人了……,即这种隐藏仅仅只是一种语法上的变形操作

print(People._People__country)
China

2.类的封装这种语法上的变形只在类定义阶段发生一次,因为类体代码仅仅只在类定义阶段检测一次

People.__x = 11
print(People.__dict__)
{'__module__': '__main__', '_People__country': 'China', '_People__n': 100, '__init__': <function People.__init__ at 0x10953af28>, 'eat': <function People.eat at 0x10953aea0>, '__dict__': <attribute '__dict__' of 'People' objects>, '__weakref__': <attribute '__weakref__' of 'People' objects>, '__doc__': None, '__x': 11}

3.类的封装是对外不对内的,即在类的内部可以直接访问,而在类的外则无法直接访问,原因是在类定义阶段,类体内代码统一发生了一次变形

peo1 = People('egon', 18, 'male')
print(peo1.__dict__)
{'_People__name': 'egon', 'age': 18, 'sex': 'male'}
peo1.__x = 111
print(peo1.__dict__)
{'_People__name': 'egon', 'age': 18, 'sex': 'male', '__x': 111}

4.如果不想让子类的方法覆盖父类的,可以将该方法名前加一个__开头

class Foo:
    def __f1(self):  # _Foo__f1
        print('Foo.f1')

    def f2(self):
        print('Foo.f2')
        self.__f1()  # self._Foo__f1


class Bar(Foo):
    def __f1(self):  # _Bar__f1
        print('Bar.f1')


obj = Bar()
obj.f2()  # f2()中的__f1()是self._Foo_f1,是父类的__f1,而不是子类的__f1
Foo.f2
Foo.f1

为什么要封装

技术分享图片

封装数据属性的目的

封装函数属性

封装的应用

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

    def tell_info(self):
        print('%s:%s' % (self.__name, self.__age))

    def set_info(self, name, age):
        if type(name) is not str:
            # print('用户名必须为str类型')
            # return
            raise TypeError('用户名必须为str类型')

        if type(age) is not int:
            # print('年龄必须为int类型')
            # return
            raise TypeError('年龄必须为int类型')
        self.__name = name
        self.__age = age
        print('%s:%s创建成功' % (self.__name, self.__age))


peo1 = People('egon', 18)
peo1.tell_info()
egon:18
peo1.set_info('egon', 19)
egon:19创建成功

技术分享图片

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()
插卡成功
用户认证成功
输入取款金额成功
打印账单成功
取款成功

模块的封装(了解)

技术分享图片

python并不会真的阻止你访问私有的属性,模块也遵循这种约定,如果模块名以单下划线开头,那么from module import *时不能被导入,但是你from module import _private_module依然是可以导入的

其实很多时候你去调用一个模块的功能时会遇到单下划线开头的(socket._socket,sys._home,sys._clear_type_cache),这些都是私有的,原则上是供内部调用的,作为外部的你,一意孤行也是可以用的,只不过显得稍微傻逼一点点

原文:https://www.cnblogs.com/nickchen121/p/10989430.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!