26, Python class inheritance and function overloading

# 类继承

class Video(object):
    # 4、私有成员变量 两个下划线开头定义的变量就是私有成员变量
    __name = "private name"
    # 3、直接声明成员变量
    # 声明的意思是这个变量的空间不产生,只有当Video生成对象的时候这个空间才产生,所以叫声明
    age = 20
    path = ""
    # 构造函数
    def __init__(self, path):
        print("Create Video")
        self.name = "public name"
        self.path = path
        print("path is", self.path)
    
    # Python约定了我们函数名全部是小写
    def get_name(self):
        return self.__name
    
    # 析构函数
    def __del__(self):
        print("Delete Video") 

# 类继承
class Mp4Video(Video):
    def __init__(self):
        print("Create Mp4Video")

mp4 = Mp4Video()
print(mp4)

Program output:

Create Mp4Video
Delete Video
<__main__.Mp4Video object at 0x000001E5ADF30390>

We can see Create Mp4Video, but it did not call the construction method of our parent class. This is not consistent with our C++. In C++, when a subclass object is generated, the construction method of the parent class is automatically called.
But in Python, the constructor of the parent class is not called. In Python, only the constructor of the child class is called, and the constructor of the parent class is not actively called.

# 类继承

class Video(object):
    # 4、私有成员变量 两个下划线开头定义的变量就是私有成员变量
    __name = "private name"
    # 3、直接声明成员变量
    # 声明的意思是这个变量的空间不产生,只有当Video生成对象的时候这个空间才产生,所以叫声明
    age = 20
    path = ""
    # 构造函数
    def __init__(self, path):
        print("Create Video")
        self.name = "public name"
        self.path = path
        print("path is", self.path)
    
    # Python约定了我们函数名全部是小写
    def get_name(self):
        return self.__name
    
    # 析构函数
    def __del__(self):
        print("Delete Video") 

# 类继承
class Mp4Video(Video):
    def __init__(self, path): # 不会主动调用父类构造函数,需要显式调用父类构造函数
        Video.__init__(self, path)
        print("Create Mp4Video")
    def __del__(self):
        print("Delete Mp4Video")

mp4 = Mp4Video("e:/test.mp4")
print("mp4.get_name()", mp4.get_name())

Program output:

Create Video
path is e:/test.mp4
Create Mp4Video
Delete Mp4Video
mp4.get_name() private name

We can see that unlike in C++, Python only calls the destructor of the subclass, it does not call the destructor of the parent class.
Inheritance means that we have all the parameters and methods of the parent class.

Sometimes we will use some polymorphism in inheritance. At this time, we need to judge what type of your object is and which class it is, how do you judge it?
Python provides a method:isinstance()

# 类继承

class Video(object):
    # 4、私有成员变量 两个下划线开头定义的变量就是私有成员变量
    __name = "private name"
    # 3、直接声明成员变量
    # 声明的意思是这个变量的空间不产生,只有当Video生成对象的时候这个空间才产生,所以叫声明
    age = 20
    path = ""
    # 构造函数
    def __init__(self, path):
        print("Create Video")
        self.name = "public name"
        self.path = path
        print("path is", self.path)
    
    # Python约定了我们函数名全部是小写
    def get_name(self):
        return self.__name
    
    # 析构函数
    def __del__(self):
        print("Delete Video") 

# 类继承
class Mp4Video(Video):
    def __init__(self, path): # 不会主动调用父类构造函数,需要显式调用父类构造函数
        Video.__init__(self, path)
        print("Create Mp4Video")
    def __del__(self):
        print("Delete Mp4Video")

mp4 = Mp4Video("e:/test.mp4")

class AviVideo(Video):
    pass

print(isinstance(mp4, Mp4Video))
print(isinstance(mp4, Video))
print(isinstance(mp4, AviVideo))

Program output:

Create Video
path is e:/test.mp4
Create Mp4Video
Delete Mp4Video
True
True
False

Through this method, when you have multiple inherited classes, you can choose according to its type, and you can judge by this, which makes it more convenient for us to design patterns.

Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/109291074