Python学习 :面向对象 -- 成员修饰符

  成员修饰符

  两种成员

  - 公有成员

  - 私有成员, __字段名

                  - 无法直接访问,只能通过内部方法来间接访问私有成员

  简例:公有成员与私有成员 

class Info:
    country = '中国'  # 静态字段    __gender = '男'   # 静态字段私有化
    def __init__(self,name,age):
        self.name = name
        self.__age = age  # age字段为私有的,外部无法直接访问
    def show(self):
        return self.__age,Info.__gender
    def __get(self):
        print('私有方法')
    def gain(self):
        r = self.__get()
        return r

obj = Info('alex',19)
print(obj.name)
# print(obj.__age)  # 此时会报错,无法访问
res = obj.show()    # 方法在类中,故能通过方法在内部访问私有字段
print(res)
# obj.__get()       # 此时也会报错,无法访问
obj.gain()          # 通过内部方法来访问
>>>>>>>>>
alex
(19, '男')
私有方法

  特殊成员

  - __init__ 类()自动执行

  - __call__ 对象() 类()() 自动执行

  - __int__ int() 执行

  - __str__ str() 执行

  - __dict__ 对象.__dict__ 执行,将对象中封装的所有内容以字典的形式返回

  简例:__call__方法

class Info:
    def __init__(self):
        print('init')

    def __call__(self, *args, **kwargs):        # 对象() 自动执行
        print('call')

obj = Info()
obj()       # 只执行__call__方法
Info()()    # 相当于 obj()()
>>>>>>>>>
init
call
init
call

  简例:__int__方法 __str__方法 __dict__方法 __getitem__方法

class Info:
    def __init__(self,name,age):
        self.name = name
        self.age  = age

    # int 对象,自动执行__int__方法,并将返回值赋值给int对象
    def __int__(self):
        return 0

    # str 对象,自动执行__str__方法,并将返回值赋值给str对象
    def __str__(self):
        return '%s - %s' %(self.name,self.age)

    def __getitem__(self, item):
        return item

obj = Info('alex',20)
print(obj)              # 实际上 print()执行print(str(obj))
d = obj.__dict__
print(d)
res = Info.__dict__     # 查看类中的内容
print(res)
li = Info('mike',22)
res = li['APPLE']             # 自动执行 li 对象的类中的 __getitem__方法,’APPLE‘作为参数传递给item
print(res)
>>>>>>>>>
alex - 20
{'name': 'alex', 'age': 20}
{'__int__': , '__getitem__': , '__str__': , '__dict__': <attribute '__dict__' of 'Info' objects>, '__init__': , '__doc__': None, '__weakref__': <attribute '__weakref__' of 'Info' objects>, '__module__': '__main__'}
APPLE

  metaclass , 类的本源 type

  python中一切的事物都是对象,在调用类的时候都会经过 type类,python中默认 metaclass = type

  简例:创建一个Mytype类来继承type类中的方法,在Mytype类中可以自定义自己需要的方法,而不必要一定执行type类中的方法

class Mytype(type):
    def __init__(self,*args,**kwargs):
        # self = Info类
        super(type, self).__init__()
    def __call__(self,*args,**kwargs):
        # self = Info类
        obj = self.__new__(self,*args,**kwargs)
        # 此时 r 为 Info类中的 __new__方法中返回的对象
        self.__init__(obj)

class Info(object,metaclass=Mytype):
    def __init__(self):
        print('hello world')
# obj = Info() 其实就是调用了类中的 __new__方法来创建obj对象 def __new__(cls,*args,**kwargs): return object.__new__(cls,*args,**kwargs) # 此时创建了对象 def func(self): print('hi world') obj = Info() >>>>>>>>> hello world

  值得注意的是obj 是对象,属于Info类

  Info类也是对象 ,只不过是type类中的对象,type其实也是一个类

  实际上类在执行阶段的执行顺序是: 对象 = 类() -- type类中的__call__方法 -- 类()中的__new__方法 -- 类中的 __init__方法 ;而并非之前所说的简单地调用了 __init__ 方法

猜你喜欢

转载自www.cnblogs.com/ArticleYeung/p/10500105.html
今日推荐