Python OOP: three object-oriented features (encapsulation, inheritance, polymorphism), class attributes and instance attributes, class methods @classmethod, static methods @staticmethod

One, the three characteristics of object-oriented

Insert picture description here

Two, polymorphism

Insert picture description here
Definition: Polymorphism is a way of using objects. Subclass overrides the class method, and calls the same class method of different subclass objects, which can produce different execution results.

A parent class can be inherited to multiple subclasses, and each different subclass can create multiple objects, and each object can call the method of the parent class of the parent class to produce different execution results.

# 犬与人协同工作,人协同不同的犬执行不同的任务
class Dog:
    def work(self):   # ⽗类提供统⼀的⽅法,哪怕是空⽅法
        print("指哪打哪")

class ArmyDog(Dog):  # 继承Dog类
    def work(self):  # ⼦类重写⽗类同名⽅法
        print("追击敌人")

class DrugDog(Dog):
    def work(self):  # ⼦类重写⽗类同名⽅法
        print("追查毒品")

class Person:
    def work_with_dog(self, dogtype):  # 传⼊不同的对象,执⾏不同的代码,即不同的work函数
        dogtype.work()

ad = ArmyDog()
dd = DrugDog()
p1 = Person()

ad.work()  # 追击敌人
p1.work_with_dog(ad)  # 追击敌人
p1.work_with_dog(dd)  # 追查毒品

Three, set and access class attributes

- class property is owned by the object class attribute, all instances of the class object class attribute is shared .

-Class attributes can be accessed with class objects or instance objects.
Insert picture description here

class Dog:
    age = 3

wangcai = Dog()
xiaohei = Dog()

print(Dog.age)  # 通过类访问类属性
print(wangcai.age)  # 通过对象访问类属性
print(xiaohei.age)  # 通过对象访问类属性

输出:
3
3
3

Fourth, modify class attributes

The class attribute can only be modified through the class object, not through the instance object . If the class attribute is modified through the instance object, it means that an instance attribute is created.

class Dog:
    age = 3

wangcai = Dog()
xiaohei = Dog()

Dog.age = 10  # 通过类修改类属性
print(Dog.age)  # 10 通过类访问类属性
print(wangcai.age)  # 10 通过对象访问类属性
print(xiaohei.age)  # 10 通过对象访问类属性
class Dog:
    age = 3

wangcai = Dog()
xiaohei = Dog()

wangcai.age = 20  # 通过对象修改类属性,实际并未修改类属性,而是创建了一个同名实例属性
print(Dog.age)  # 3 通过类访问类属性
print(wangcai.age)  # 20 通过对象访问类属性
print(xiaohei.age)  # 3 通过对象访问类属性

Five, instance attributes

Instance attributes cannot be accessed through the class.

  • Each attribute of each object occupies a separate memory space. If a large number of instance attributes are defined, memory will be consumed.
  • Some properties whose values ​​do not change can be defined as class properties. Class properties share a share of memory, which can reduce memory consumption.
  • If you don't even need to pass parameters, you can define static methods to further reduce memory consumption.
class Dog:
    def __init__(self):
        self.age = 5

    def printinfo(self):
        print(self.age)
        
wangcai = Dog()
print(wangcai.age)  # 对象访问实例属性
wangcai.printinfo()  # 对象访问实例方法
print(Dog.age)  # 报错:实例属性不能通过类访问

Insert picture description here

Six, class methods

1. The characteristics of the class method

  • The first parameter is the method of the class object
  • The decorator @classmethod needs to be used to mark it as a class method . For a class method, the first parameter must be a class object. Generally, cls is used as the first parameter, and cls represents this class.

@classmethod, a function of the identification of a class method
`

2. Class method usage scenarios

  • When the method needs to use the class object (such as accessing private class attributes, etc.), define the class method
  • Class methods are generally used in conjunction with class attributes
class Dog:
    __age = 3  #  添加私有类属性,两道下划线.属性名=属性值

    @classmethod    # 定义类方法
    def get_age(cls):  # 形参cls代表Dog类(在实例属性里用形参self代表实例对象)
        return cls.__age  # 访问类属性的值
    
wangcai = Dog()
result = wangcai.get_age()  # 对象调用类方法
print(result)  # 3
print(Dog.get_age())  # 3 类调用类方法

Seven, static method

1. The characteristics of static methods

  • It needs to be decorated with the decorator @staticmethod . Static methods do not need to pass class objects nor instance objects (the formal parameters do not have self/cls) .
  • Static methods can use both object access and class access .

2. Static method usage scenarios

  • When the method neither needs to use instance objects (such as instance objects, instance attributes) nor class objects (such as class attributes, class methods, create instances, etc.), static methods can be defined
  • Canceling unnecessary parameter transfer is conducive to reducing unnecessary memory usage and performance consumption
class Dog:
    @staticmethod
    def printinfo():
        print("这是一个犬类,用于创建犬实例……")

wangcai = Dog()
wangcai.printinfo()  # 通过对象调用类方法
Dog.printinfo()  # 通过类调用类方法

输出:
这是一个犬类,用于创建犬实例……
这是一个犬类,用于创建犬实例……

8. Summary

Insert picture description here

PS: source,itheima

Guess you like

Origin blog.csdn.net/weixin_47008635/article/details/114748867