Class inheritance
1. Define class inheritance
First of all, let’s look at the basic syntax of class inheritance:
class ClassName(BaseClassName): <statement-1> . . . <statement-N>
When defining a class, you can put it in parentheses When writing inherited classes, as mentioned at the beginning, if you do not need to inherit classes, you must also write inherited object classes, because in Python the object class is the parent class of all classes.
Of course the above is single inheritance, Python also supports multiple inheritance. The specific syntax is as follows:
class ClassName(Base1,Base2,Base3): <statement-1> . . . <statement-N>
There is one thing to note about multiple inheritance: if there are the same method names in the parent class, If it is not specified when using a subclass, Python will search from left to right in the order of the parent class in parentheses. That is, if the method is not found in the subclass, it will search from left to right to see if the method is included in the parent class.
So what can inherited subclasses do?
Benefits of inherited subclasses:
will inherit the properties and methods of the parent class
You can define them yourself and override the properties and methods of the parent class
2. Call the method of the parent class
After a class inherits the parent class, it can directly call the method of the parent class. For example, in the following example, UserInfo2 inherits from the parent class UserInfo and can directly Call the get_account method of the parent class.
#!/usr/bin/env python # -*- coding: UTF-8 -*- class UserInfo(object): lv = 5 def __init__(self, name, age, account): self.name = name self._age = age self.__account = account def get_account(self): return self.__account class UserInfo2(UserInfo): pass if __name__ == '__main__': userInfo2 = UserInfo2('两点水', 23, 347073565); print(userInfo2.get_account())
3. Overriding of parent class methods
Of course, you can also override parent class methods.
Example:
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- class UserInfo(object): lv = 5 def __init__(self, name, age, account): self.name = name self._age = age self.__account = account def get_account(self): return self.__account @classmethod def get_name(cls): return cls.lv @property def get_age(self): return self._age class UserInfo2(UserInfo): def __init__(self, name, age, account, sex): super(UserInfo2, self).__init__(name, age, account) self.sex = sex; if __name__ == '__main__': userInfo2 = UserInfo2('两点水', 23, 347073565, '男'); # 打印所有属性 print(dir(userInfo2)) # 打印构造函数中的属性 print(userInfo2.__dict__) print(UserInfo2.get_name())
The last printed result:
Here is the constructor of the parent class that has been rewritten.
4. Type judgment of subclasses
For the inheritance relationship of class, sometimes we need to judge the type of class. What should we do?
You can use the isinstance() function.
You can understand the usage of the isinstance() function with an example.
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- class User1(object): pass class User2(User1): pass class User3(User2): pass if __name__ == '__main__': user1 = User1() user2 = User2() user3 = User3() # isinstance()就可以告诉我们,一个对象是否是某种类型 print(isinstance(user3, User2)) print(isinstance(user3, User1)) print(isinstance(user3, User3)) # 基本类型也可以用isinstance()判断 print(isinstance('两点水', str)) print(isinstance(347073565, int)) print(isinstance(347073565, str))
The output results are as follows:
True True True True True False
You can see that isinstance() can not only tell us whether an object is of a certain type, but can also be used to judge basic types.