Python Object-Oriented Programming (checking for gaps)


1. Packaging

Encapsulation : Encapsulation of properties and methods into an abstract class.

1. Call method

The code is as follows (example):

class Student():
    def study(self,course_name):
        print(f'学生正在学习{course_name}')
    def play(self):
        print('学生正在玩游戏')
stu1 = Student()
stu2 = Student()

stu1.study('python')
#调用类方法,需要对类进行实例化
Student().study('Java')

2. Initialization method

  • init () method: (the underline cannot be displayed) When creating an object, the memory space required to save the object will be obtained in memory, and then the initialization method will be automatically executed to complete the memory initialization method. In short, it is to bind properties and assign initial values ​​​​to objects when creating objects.
  • Call: the attribute is called with self, and the parameter is called directly

3. Attribute decorator

The code is as follows (example):

#属性装饰器
class Student():
    def __init__(self,name,age):
        #创建私有属性
        self.__age = age
        self.__name = name
        
    #获取私有属性
    @property
    def name(self):
        return self.__name
    #修改私有属性
    @name.setter
    def name(self,name):
        self.__name=name or 'unknown'
    @property
    def age(self):
        return self.__age

    def study(self,course_name):
        print(f'{self.__name}正在学习{course_name}')

stu3=Student('王大锤',50)
print(stu3.name,stu3.age)

4. Method

  • Static method: When calling, there is no need to instantiate the class, you can directly use: class name. method ()
  • Static method vs class method:
    1. Parameters: static parameter passing—parameter, class method passing parameter—the first must be cls
    2. Function: static method is not bound to the current class, it can be regarded as a separate function, The class method represents that the current method belongs to the current class
    3. Call: Static methods can be called without creating objects, class methods cannot be called through instance objects, and instance methods cannot be called through class objects.
     

2. Inheritance

Inheritance : It is to achieve code reuse, and the same code does not need to be written repeatedly.

1. Inheritance syntax

The code is as follows (example):

class 类名(父类名)
    pass

2. Method rewriting

When the subclass wants to add new functions, but the parent class cannot modify the functions, method rewriting can be used .

class Dog(): 
    def bark(self):
        print('bark!')
        
class Xiaotianquan(Dog):
    def fly(self):
        print("fly!")
    #方法的重写
    def bark(self):
        print('bark as a god!')

3. Call the parent class method

There are two ways to call parent class methods:

class Xiaotianquan(Dog):
    def fly(self):
        print("fly!")
    def bark(self):
        print('bark as a god!')
        #调用父类方法1,super()是个对象
        super().bark()
        
        #调用父类方法2
        Dog.bark(self)

 

Three, polymorphism

Polymorphism means that different subclass objects call the same parent class method, resulting in different execution results.

class A:
    def work(self):
        print('人类需要工作')
class B(A):
    def work(self):
        print('程序员在工作--------------代码')
class C(A):
    def work(self):
        print('设计师在工作--------------图纸')

b=B()
c=C()
b.work()
c.work()

 

4. Singleton design pattern

This is to let the objects created by the class have only one instance in the system and the memory address is the same. The implementation code of the singleton design pattern that is initialized only once is as follows:

class MusicPlayer:
    instance = None
    init_Flag = False
    def __new__(cls) :
        if cls.instance is None:
            cls.instance = super().__new__(cls)
        return cls.instance
    def __init__(self):
        if MusicPlayer.init_Flag:
            return
        print('初始化中....')
        MusicPlayer.init_Flag=True

player1 = MusicPlayer()
player2 = MusicPlayer()
print(player1,player2)

Guess you like

Origin blog.csdn.net/qq_42804713/article/details/129231566