zoukankan      html  css  js  c++  java
  • Python 类的特殊成员介绍

    类的成员有两种形式

    公有成员,在任何地方都能访问
    私有成员,只有在类的内部才能方法,私有成员命名时,前两个字符是下划线。

    class Foo:
        def __init__(self, name, age):
            self.name = name
            self.__age = age
    
        def show(self):            # 间接方法私有字段
            return self.__age
        
    
    obj = Foo('klvchen', 25)
    print(obj.name)
    res = obj.show()
    print(res)
    
    运行结果:
    klvchen
    25
    

    公有静态字段:类可以访问;类内部可以访问;派生类中可以访问
    私有静态字段:仅类内部可以访问;

    class Foo:
        __v = '666'         # 私有静态字段
    
        def __init__(self):
            pass
    
        def show(self):
            return Foo.__v
    
    obj = Foo()
    res = obj.show()
    print(res)
    
    运行结果:
    666
    
    class Foo:
        __v = '666'
    
        def __init__(self):
            pass
    
        def show(self):
            return Foo.__v
    
        @staticmethod
        def stat():
            return Foo.__v
    
    
    res = Foo.stat()
    print(res)
    
    运行结果:
    666
    

    无法从父类继承私有字段

    class F:
        def __init__(self):
            self.ge = 123
            self.__gene = 456     #私有字段
    
    
    class S(F):
        def __init__(self, name):
            self.name = name
            self.__age = 18
            super(S, self).__init__()
    
        def show(self):
            print(self.name)
            print(self.__age)
            print(self.ge)
            print(self.__gene)
    
    
    s = S('klvchen')
    s.show()
    
    运行结果:
    
    klvchen
    18
    123
    AttributeError: 'S' object has no attribute '_S__gene'
    

    类的特殊成员

    int(对象),会自动执行对象中的__int__方法,并将返回赋值给 int 对象,同理 str(对象),会自动执行__str__方法,并返回赋值给 str 对象。

    class Foo:
        def __init__(self):
            pass
    
        def __int__(self):
            return 666
    
        def __str__(self):
            return 'hello world'
    
    obj = Foo()
    
    print(obj, type(obj))
    res = int(obj)
    print(res)
    res1 = str(obj)
    print(res1)
    
    运行结果:
    <__main__.Foo object at 0x0000022BBE9DA978> <class '__main__.Foo'>
    666
    hello world
    

    print(对象),str(对象),都会自动执行对象中的__str__方法,并返回

    class Foo:
        def __init__(self, n, a):
            self.name = n
            self.age = a
    
        def __str__(self):
            return '%s-%d' %(self.name, self.age)
    
    obj = Foo('klvchen', 28)
    print(obj)
    
    运行结果:
    klvchen-28
    

    两个对象相加时,自动执行第一对象的__add__方法,并且将第二个对象当参数传递进去

    class Foo:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def __add__(self, other):
            return self.age + other.age
    
    obj1 = Foo('klv1', 23)
    obj2 = Foo('klv2', 24)
    
    res = obj1 +  obj2
    print(res, type(res))
    
    运行结果:
    47 <class 'int'>
    
    class Foo:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def __add__(self, other):
            return Foo(obj1.name, obj2.age)
    
        def __del__(self):
            print('析构方法')
    
    obj1 = Foo('klv1', 23)
    obj2 = Foo('klv2', 24)
    
    res = obj1 + obj2
    print(res, type(res))
    
    运行结果:
    <__main__.Foo object at 0x0000016DFCE125C0> <class '__main__.Foo'>
    析构方法
    析构方法
    析构方法
    

    li[对象] 会自动执行 li 对象的类中的__getitem__方法,8当作参数传递给item

    class Foo:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def __getitem__(self, item):
            return item
    
    li = Foo('klvchen', 28)
    r = li[8]
    print(r)
    
    运行结果:
    8
    

    类中的__setitem__,__delitem__方法

    class Foo:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def __getitem__(self, item):
            return item
    
        def __setitem__(self, key, value):
            print(key, value)
    
        def __delitem__(self, key):
            print(key)
    
    li = Foo('klvchen', 28)
    r = li[8]
    print(r)
    
    li[100] = 'hello'
    
    del li[999]
    
    运行结果:
    8
    100 hello
    999
    

    执行类中的__iter__()方法,并获取其返回值,循环上一步中返回对象,用于迭代器,之所以列表、字典、元组可以进行for循环,是因为类型内部定义了 iter

    class Foo:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def __iter__(self):
            return iter([11, 22, 33])
    
    
    li = Foo('klvchen', 26)
    
    for i in li:
        print(i)
    
    运行结果:
    11
    22
    33
    

    for 循环的内部操作

    obj = iter([11, 22, 33])
    
    while True:
        val = obj.next()
        print val
    
  • 相关阅读:
    [转载] boost安装笔记——vs2008
    CSS 中的分离、抽象和层叠(转载)
    web server大全之GoAhead移植(转载)
    MIT牛人解说数学体系(转载)
    CodeBlocks 编辑器设置绿色背景
    Eclipse+Tomcat+Mysql构建J2EE开发环境(转载)
    堆和栈的区别——给初学者 (装载)
    Eclipse下设置github开发环境
    [原创] MyEclipse配置Tomcat和MySQL
    VS2008下编译Boost(转载+更改)
  • 原文地址:https://www.cnblogs.com/klvchen/p/9189384.html
Copyright © 2011-2022 走看看