~ Object-oriented class members: members of the private, public members, instance methods, class methods, static methods, properties (property), isinstance, issubclass, metaclass (type)

A private members of the public members

  • Public Member: can access anywhere

  • Internal access only to the class: private members

    From the class is loaded, as long as the encounter class private members, will be in front of the private members of the class name plus _

Examples of the method two

Examples Examples of the method is a method that can be used the class

Three methods

Method class decorator @classmethod

Class methods: the general method is through the class name to call, and automatically address to the class name cls,

But if the object can also call, but pass the address or name of the class address.

class Student:
    count = 0
    def __init__(self,name,id):
        self.name = name
        self.id = id
        Student.stu_num()

    @classmethod
    def stu_num(cls):
        cls.count += 1
    @classmethod
    def get_num(cls):
        print(f'学生的数量是{cls.count}')

xiaohu = Student('小虎',123)
# xiaohu.get_num()
Student.get_num()

The role of class methods:

  1. To give the object can be instantiated class name.
  2. Class attribute can operate.

Four static methods

Static method uses a decorator @staticmethod.

Static methods are not dependent on the class of the object, the operation does not involve the properties and methods of classes. Can be understood as a static method is independent, a simple function

Normative ensure that the code, rational division. Follow-up maintenance is high.

import time

class TimeTest(object):
    def __init__(self, hour, minute, second):
        self.hour = hour
        self.minute = minute
        self.second = second

    @staticmethod
    def showTime():
        return time.strftime("%H:%M:%S", time.localtime())

print(TimeTest.showTime())
t = TimeTest(2, 10, 10)
nowTime = t.showTime()
print(nowTime)

Five attributes property

The dynamic approach masquerading as a property, although there is no lift at the code level, but make you look more reasonable.

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

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

    @AAA.deleter
    def AAA(self):
        print('delete的时候运行我啊')
#只有在属性AAA定义property后才能定义AAA.setter,AAA.deleter
f1=Foo()
f1.AAA  # 执行property
f1.AAA='aaa' # 执行AAA.setter
del f1.AAA  # 执行AAA.deleter
class Foo:
    def get_AAA(self):
        print('get的时候运行我啊')

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

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

f1=Foo()
f1.AAA
f1.AAA='aaa'
del f1.AAA

六 isinstance issubclass

isinstance (a, B): it is determined whether a Class B (Class B, or a derived class) instantiated objects

issubclass (A, B): A class is determined whether class B (or B derived class) derived classes

Seven yuan class type

Gets the object belongs to a class

print(type('abc'))

Python principle is: everything is an object, in fact, classes can also be understood as 'objects', also known as class and type yuan to build classes, python most of the built-in classes (including object) as well as their own definition of class, all the type yuan class creation.

The relationship between the type classes and more unique object class: object is an instance of the class type, and the type is a subclass of the class of the object class, comparing the relationship expressed python magic can not be used in the code, because the definition of which must be present before the other one .

Guess you like

Origin www.cnblogs.com/lav3nder/p/11802036.html