3.Python面向对象(三)

"""
一、成员修饰符
    共有成员

    私有成员
        __字段名 无法直接访问,只能间接访问
        __方法名 
    
    class clsName:

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


二、特殊成员
    
    类名()            执行 __init__()  构造方法
    __del__ 析构方法 对象被销毁的时候,自动执行

    对象()或类名()() 执行 __call__()
    int(对象)         执行 __int__()
    str(对象)         执行 __str__()
    
    __add__
    
    __dict__      #将对象中封装的所有内容通过字典的形式返回

    __getitem__  对象[item]、对象[start:stop:step]  #切片(slice类型)或者索引

    __setitem__  对象[key] = val

    __delitem__  del 对象[key]

    __iter__  类中有__iter__方法,对象=》可迭代对象
    对象.__iter__()的返回值:迭代器
    for 循环,迭代器.next()
    for 循环,可迭代对象.__iter__() 迭代器 next

三、metaclass,类型的祖宗
    一切皆对象
    类都是type类的对象 type(...)
    类的对象 类名()
    
    type类中的__init__方法

    
    class Foo:

        def func(self):
            print(self)
    
    obj = Foo()

    ==============================

    def function(self):
        print(self)

    Foo = type('Foo',(object,),{'func':function})
    obj = Foo()
    
    ==============================

    Foo = type('Foo',(object,),{'func':lambda x:123})

"""

class MyType(type):

    def __init__(self,*args,**kwargs):
        print(self,"MyType __init__")

    def __call__(self,*args,**kwargs):
        print(self,"MyType __call__")
        obj = self.__new__(self,*args,**kwargs)
        self.__init__(obj,*args,**kwargs)
        return obj


class Person(object,metaclass=MyType):

    __country = "中国"

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

    # 真正的对象是在__new__里面创建的
    def __new__(cls,*args,**kwargs):
        print(cls,"Person Object __new__")
        return object.__new__(cls)

    def __call__(self,*args,**kwargs):
        print(self,"Person __call__")

    def __reading(self):
        print(self,"Person private __reading")

    def read(self):
        return self.__reading()

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


class Mother(Person):
    """
        我是注释...
    """
    def __init__(self,name,age):
        super(Mother,self).__init__(name,age)

    #真正的对象是在__new__里面创建的
    def __new__(cls,*args,**kwargs):
        print(cls,"Mother Object __new__")
        return object.__new__(cls)

    def __call__(self,*args,**kwargs):
        print(self,"Mother __call__")


    def __int__(self):
        return 110

    def __str__(self):
        return "%s:%s"%(self.name,self.age)

    def __getitem__(self,item):
        print(self,item,type(item))
        if type(item) == slice:
            return (item.start,item.stop,item.step)
        else:
            return item + 10        

    def __setitem__(self,key,val):
        print(self,key,val)

    def __delitem__(self,key):
        print(self,key)

    def __iter__(self):
        return iter([11,22,33,44,55])

    def show(self):
        print(self.name,self.age)




"""
程序从上至下执行加载进内存执行。
<class '__main__.Person'> MyType __init__
<class '__main__.Mother'> MyType __init__

Mother("Lily",18)  Mother是MyType/type类的对象
对象(MyType/type类的)()--->__call__
<class '__main__.Mother'> MyType __call__
Mother的__new__()
<class '__main__.Mother'> Mother Object __new__
Mother的__init__()
one str(one)->__str__(self)
print(one)--->Lily:18

Mother("Faker",20) Mother是MyType/type类的对象
对象(MyType/type类的)()--->__call__
<class '__main__.Mother'> MyType __call__
Mother的__new__()
<class '__main__.Mother'> Mother Object __new__
Mother的__init__()
two str(two)->__str__(self)
print(two)--->Faker:20

"""

one = Mother("Lily",18)
two = Mother("Faker",20)

print(one)
print(two)


# reading 私有不能直接访问
# one.read()

# one.show()

# 对象() __call__ 方法
# one()
# int(one) -> __int__(self)
# print(int(one))
# one str(one)->__str__(self)
# print(one)

# print(Mother.__dict__)
# print(one.__dict__)
# 
# print(one[10])
# print(one[1:5:2])
# one[10] = 30
# del one[10]

"""
__iter__  类中有__iter__方法,对象=》可迭代对象
    对象.__iter__()的返回值:迭代器
    for 循环,迭代器.next()
    for 循环,可迭代对象.__iter__() 迭代器 next
"""
# for item in one:
#   print(item)
#

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容