Class Properties and Instance Properties Class Methods and Static Methods Stage 1 Core Programming in Python: Object-oriented Programming Other 014 one. The three main features of object-oriented encapsulation will write attributes and methods into the operation of the class is encapsulation encapsulation can add private permissions for attributes and methods inherit subclasses default to inherit all the attributes and methods of the parent class subclasses can override the parent class attributes and methods polymorphic passed into different objects, produce different results. Polymorphism 2.1 Understand polymorphism Polymorphism refers to a class of things that have many forms (an abstract class has many subclasses, so the concept of polymorphism depends on inheritance).

Definition: polymorphism is a way to use the object, subclass to rewrite the parent class method, call different subclass object of the same parent class method, can produce different results of execution benefits: call flexible, with polymorphism, it is easier to write a common code, make common programming, in order to adapt to the changing needs! Class Dog(object): def work(self): Print (‘ print where… ‘)

Class ArmyDog(Dog): def work(self): print(‘ chase the enemy… ‘)

Class DrugDog(Dog): def work(self): print(‘ Trace drugs… ‘)

Class Person(object): def work_with_dog(self, dog): class Person(object): def work_with_dog(self, dog)

ad = ArmyDog() dd = DrugDog()

Daqiu = Person() daqiw.work_with_dog (AD) daqiw.work_with_dog (dd) 三. Class Properties and Instance Properties 3.1 Class Properties 3.1.1 Setting and Accessing Class Properties A class property is a property owned by a class object, which is shared by all instance objects of that class. Class properties can be accessed using class objects or instance objects. class Dog(object): tooth = 10

wangcai = Dog() xiaohei = Dog()

Print (dog.tooth) # 10 print(wangcai.tooth) # 10 print(xiaohei.tooth) # 10

Class attributes are defined when an item of record data is consistently consistent.

Instance attributes require each object to carve out a separate memory space for recording data, while class attributes are shared by all classes and occupy only one memory, saving more memory space.

3.1.2 Modifying Class Attributes A class attribute can only be modified by a class object, not by an instance object. If a class attribute is modified by an instance object, an instance attribute is created.

class Dog(object): tooth = 10

wangcai = Dog() xiaohei = Dog()

Modifying class attributes

Dog.tooth = 12 print(Dog.tooth) # 12 print(wangcai.tooth) # 12 print(xiaohei.tooth) # 12

You cannot modify a property through an object; if you do so, you are creating an instance property

Wangcai.tooth = 20 print(dog.tooth) # 12 print(xiaohei.tooth) # 12 def init(self): self.age = 5

def info_print(self):
    print(self.age)
Copy the code

wangcai = Dog() print(wangcai.age) # 5

Print (dog.age) # print(dog.age) #

Wangcai.info_print () # 5 四. Class methods and Static methods 4.1 Class methods 4.1.1 Class methods need to be identified as class methods by the decorator @classMethod. For class methods, the first parameter must be a class object. CLS is usually used as the first parameter. 4.1.2 Class Method Usage Scenarios When a method needs to use class objects (such as accessing private class attributes), define class methods. Class methods generally use class Dog(object): __tooth = 10 together with class attributes

@classmethod
def get_tooth(cls):
    return cls.__tooth
Copy the code

Wangcai = Dog() result = wangcai.get_tooth() print(result) # 10 wangcai = Dog() result = wangcai.get_tooth() print(result) # 10 Static methods need to pass neither class objects nor instance objects (parameters do not have self/ CLS). Static methods can also be accessed through instance objects and class objects. 4.2.2 Scenarios for Using Static Methods When neither instance objects (such as instance objects and instance attributes) nor class objects (such as class attributes, class methods and instance creation) are needed in a method, static methods are defined to cancel unnecessary parameter passing. Class Dog(object): @staticMethod def info_print(): print(‘ this is a Dog class used to create Dog instances…. ‘)

wangcai = Dog()

Static methods can use both object access and class access

Wangcai. Info_print () t info_print (5). Classmethod @classmethod def xx(): code staticmethod @staticmethod def xx(): code