python 25 members of the class

1. The private members of the class - __

当你遇到重要的数据,功能,(只允许本类使用的一些方法,数据)将其设置成私有成员.

Private static class properties 1.1

__name = "meet"    # 加 __

#只能在类的内部进行访问、调用,在类的外部以及派生类不能调用。

1.2 private methods the class

class A:
    def __func(self):
        print("in __func")

# 只能在类的内部调用

Private methods 1.3 object

class A:
    def __init__(self, name):
        self.__name = name
        
# 只能在类的内部调用
# 总结:
类的私有成员,只能在类的内部进行访问、调用,不能在类的外部、子类使用。

python所有的私有成员都是纸老虎,形态虚设。
类在加载时,只要遇到类的私有成员(__),都会在私有成员前加上 _类名;
可以这样查看,但不能这样做。

Other methods 2. class

2.1 class method @classmethod

类方法是将类本身作为对象进行操作的方法。
类方法: 一般就是通过类名去调用的方法,并且自动将类的地址传给cls,
但是如果通过对象调用也可以,但是传的地址还是类名地址.
# 实例化一个对象,记一次数,统计学生个数。
class Student:
    count = 0

    def __init__(self, name, id):
        self.name = name
        self.id = id
        Student.num_student()   # 调用

    @classmethod
    def num_student(cls):       # cls 接受类的地址
        cls.count += 1

    @classmethod
    def getnum(cls):
        return cls.count


obj1 = Student("alex", 25412)
obj2 = Student("meet", 25413)
print(Student.getnum())
类方法的作用:
    1. 得到类名可以实例化对象;
    2. 可以操作类的属性。

2.2 static method @staticmethod

不依赖对象与类,其实静态方法就是一个函数。 
参数可随意,没有“self”和“cls”参数,但是方法体中不能使用类或实例的任何属性和方法;调用还需函数名。
保证代码规范性,合理划分,以及后续维护性高。
class A:
    @staticmethod
    def func():
        print("这是静态方法!")
print(A.func())

3. property @property

工作中如果遇到一些类似于属性的方法名,可以让其伪装成属性。
property将执行一个函数需要 函数名() 变换成 函数名。
将动态方法伪装成一个属性,使其看起来更合理。

3.1 decorator law - to acquire, modify, delete

class Foo:
    @property
    def AAA(self):
        print('get的时候运行我啊')

    @AAA.setter
    def AAA(self,value):
        print(value)
        print('set的时候运行我啊')

    @AAA.deleter
    def AAA(self):
        print('delete的时候运行我啊')
obj = Foo()
obj.AAA         # 不加()
obj.AAA = "aaa"  # 操作命令,不是改变AAA的值,而是执行被AAA.setter装饰器装饰的函数
del obj.AAA     # 操作命令,不是删除AAA,而是执行被AAA.deleter装饰器装饰的函数

3.2 instantiating objects using attributes provided

class Foo:
    def get_AAA(self):
        print('get的时候运行我啊')

    def set_AAA(self,value):
        print(value)
        print('set的时候运行我啊')

    def delete_AAA(self):
        print('delete的时候运行我啊')
        
    AAA=property(get_AAA,set_AAA,delete_AAA) 
    #内置property三个参数与get,set,delete一一对应

obj=Foo()
obj.AAA     # 不加()
obj.AAA='aaa'   # 操作命令,不是改变AAA的值,而是执行set_AAA函数
del obj.AAA     # 操作命令,不是删除AAA,而是执行delete_AAA函数

4. The built-in function of the class

Analyzing the relationship between the object and the class - 4.1 isinstance

class A:
    pass
class B(A):
    pass
class C(B):

obj = B()
print(isinstance(obj, B))    # True
print(isinstance(obj, A))   # True
print(isinstance(obj, C))   # False

# isinstance(obj,class) 判断obj是否是class类的实例化对象,或者obj是否是class派生类的实例化对象,返回Ture/False.

4.2 issubclass - Analyzing the relationship between classes and class

class A:
    pass

class B(A):
    pass

class C(B):
    pass
 
print(issubclass(B, A))     # True
print(issubclass(C, A))     # True
print(issubclass(B, C))     # False

# issubclass(cls1,cls2) 判断cls1类是否是cls2的派生类,或者是否是cls2派生类的派生  (判断cls1类是否是cls2的子孙类)

Guess you like

Origin www.cnblogs.com/yzm1017/p/11324084.html