当前位置: 移动技术网 > IT编程>脚本编程>Python > Python学习 :面向对象 -- 成员修饰符

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

2019年03月10日  | 移动技术网IT编程  | 我要评论

曾恩琦,骑马与砍杀12世纪,妈咪qvod

  成员修饰符

  两种成员

  - 公有成员

  - 私有成员, __字段名

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

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

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__ 方法

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网