zoukankan      html  css  js  c++  java
  • Python入门day28——封装

    一:封装介绍

    封装:面向对象三大特征 最核心 的一个特性
    封装 <=> 整合

    二:将封装的属性进行隐藏操作

    1.如何隐藏:在属性名前加__前缀,就会实现一个对外隐藏属性效果

    该隐藏需要注意的问题:

    I:在类外部无法直接访问双下滑线开头的属性,但知道了类名和属性名就可以拼出名字:类名__属性,然后就可以访问了,如Foo._A__N,所以说这种操作并没有严格意义上地限制外部访问,仅仅只是一种语法意义上的变形。

    class Foo:
        __x = 1  # _Foo__x
    
        def __f1(self):  # _Foo__f1
            print('from test')
    
    
    print(Foo.__dict__)     # {'__module__': '__main__', '_Foo__x': 1, '_Foo__f1': <function Foo.__f1 at 0x00C683D0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None}
    print(Foo._Foo__x)      # 1
    print(Foo._Foo__f1)     # <function Foo.__f1 at 0x00C683D0>
    

    II:这种隐藏对外不对内,因为__开头的属性会在检查类体代码语法时统一发生变形

    class Foo:
        __x = 1  # _Foo__x = 1
    
        def __f1(self):  # _Foo__f1
            print('from test')
    
        def f2(self):
            print(self.__x) # print(self._Foo__x)
            print(self.__f1) # print(self._Foo__f1)
    
    # print(Foo.__x)        # AttributeError: type object 'Foo' has no attribute '__x'
    # print(Foo.__f1)       # AttributeError: type object 'Foo' has no attribute '__f1'
    obj=Foo()               # 1
    obj.f2()                # <bound method Foo.__f1 of <__main__.Foo object at 0x0143B070>>
    class Foo:
        __x = 1  # _Foo__x = 1
    
        def __f1(self):  # _Foo__f1
            print('from test')
    
        def f2(self):
            print(self.__x) # print(self._Foo__x)
            print(self.__f1) # print(self._Foo__f1)
    
    Foo.__y=3
    print(Foo.__dict__)     # {'__module__': '__main__', '_Foo__x': 1, '_Foo__f1': <function Foo.__f1 at 0x033A8418>, 'f2': <function Foo.f2 at 0x033A83D0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None, '__y': 3}
    print(Foo.__y)          # {'__module__': '__main__', '_Foo__x': 1, '_Foo__f1': <function Foo.__f1 at 0x033A8418>, 'f2': <function Foo.f2 at 0x033A83D0>, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__doc__': None, '__y': 3}
    class Foo:
        __x = 1  # _Foo__x = 1
    
        def __init__(self, name, age):
            self.__name = name
            self.__age = age
    
    
    obj = Foo('egon', 18)
    print(obj.__dict__)           # {'_Foo__name': 'egon', '_Foo__age': 18}
    # print(obj.name, obj.age)        # AttributeError: 'Foo' object has no attribute 'name'
    
    2.为何要隐藏

    I:隐藏数据属性"将数据隐藏起来就限制了类外部对数据的直接操作,然后类内应该提供相应的接口来允许类外部间接地操作数据,接口之上可以附加额外的逻辑来对数据的操作进行严格地控制:

    # 设计者:egon
    class People:
        def __init__(self, name):
            self.__name = name
    
        def get_name(self):
            # 通过该接口就可以间接地访问到名字属性
            # print('小垃圾,不让看')
            print(self.__name)
    
        def set_name(self,val):
            if type(val) is not str:
                print('小垃圾,必须传字符串类型')
                return
            self.__name=val
    
    # 使用者:王鹏
    obj = People('egon')
    # print(obj.name) # 无法直接用名字属性
    # obj.set_name('EGON')
    obj.set_name(123123123)
    obj.get_name()
    # II、隐藏函数/方法属性:目的的是为了隔离复杂度
    
  • 相关阅读:
    asp的多国语言构思
    制作IE和FF都支持的无限级关联菜单
    破解网络尖兵(真正对付限制ADSL路由共享的方法)
    Asp透过系统DSN链接mysql数据库
    找到了一首曾经很喜欢的老歌
    生意人应具备的性格
    简单的操作让你的迅雷变的清爽
    线路分流自动跳转代码
    通过regsvr32注册DLL可以解决的一些疑难杂症
    页面无刷新超时自动退出
  • 原文地址:https://www.cnblogs.com/yding/p/12661905.html
Copyright © 2011-2022 走看看