This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
1. The concept of inheritance
Inheritance in life generally refers to the inheritance of property by children.
[img- VA07tCfi-1633508003967] [img- VA07tCfi-1633508003967]
- Extension 1: Classic or old class
Classes that are not derived from any built-in type are called classical classes.
Class Class name: code......Copy the code
- Extension 2: New class
Class Class name (object): codeCopy the code
Python object-oriented inheritance refers to the ownership of multiple classes, that is, a subclass inherits all the attributes and methods of its parent class by default, as follows:
# class A(object): def __init__(self): self.num = 1 def info_print(self): print(self.num) # class B(A): def __init__(self): self.num = 1 pass result = B() result.info_print() # 1Copy the code
In Python, all classes by default inherit from Object, which is either a top-level class or a base class. Other subclasses are called derived classes.
Two. Single inheritance
Story line: an old jianbing fruit master, in the jianbing fruit industry for many years, developed a set of exquisite jianbing fruit stall technology. The master was going to teach this skill to his single most proud disciple.
Analysis: Do apprentices inherit all of their master’s skills?
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Print (f' use {self.kongfu} to make kongfu ') # 2. Class Prentice(Master): Pass # 3. Daqiu = Prentice() # 4. Print (daqiu.kongfu) # 5. Object calls instance method daqiu.make_cake()Copy the code
Three. Multiple inheritance
Daqiu is a good boy who loves learning and wants to learn more pancake technology. Therefore, he found Bobin Education in Baidu and applied for a class to learn pancake technology.
Multiple inheritance means that a class inherits more than one parent class at a time.
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Class School(object): def __init__(self): print(f' use {self. Kongfu} to make kongfu ') Self. Kongfu = 'def make_cake(self): print(self. Kongfu =' self. pass daqiu = Prentice() print(daqiu.kongfu) daqiu.make_cake()Copy the code
Note: When a class has more than one parent, the attributes and methods of the first parent are used by default.
Subclasses override methods and attributes of the same name as their parent class
Story: Daqiu has mastered the techniques of master and training, and has devoted himself to researching his own unique recipe for a new set of pancake technology.
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Print (f' use {self.kongfu} to make kongfu ') class School(object): def __init__(self): Self. Kongfu = 'def make_cake(self): print(' self. Kongfu ') # Def __init__(self): self. Kongfu = ' 'def make_cake(self): Daqiu = Prentice() print(daqiw.kongfu) daqiw.make_cake () print(Prentice.__mro__)Copy the code
Subclasses and superclasses have attributes and methods of the same name, and the default is to use the attributes and methods of the subclass.
Subclasses call methods and attributes of the parent class with the same name
Story: Many customers want to eat the traditional method and Bo Bin’s technology of jianbing fruit.
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Print (f' use {self.kongfu} to make kongfu ') class School(object): def __init__(self): Self. Kongfu = 'def make_cake(self): print(' self. Kongfu ') class Prentice(School, Master): Def __init__(self): self. Kongfu = ' 'def make_cake(self): Self.__init__() print(f' use {self. Kongfu} to make kongfu ') # call the parent class method, Def make_master_cake(self) def make_master_cake(self) def make_master_cake(self) Master.__init__(self) Master.make_cake(self) def make_school_cake(self): School.__init__(self) School.make_cake(self) daqiu = Prentice() daqiu.make_cake() daqiu.make_master_cake() daqiu.make_school_cake() daqiu.make_cake()Copy the code
Six. Multi-level inheritance
Story: After years, Daqiu is old and wants to pass on all the techniques to his disciples.
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Print (f' use {self.kongfu} to make kongfu ') class School(object): def __init__(self): Self. Kongfu = 'def make_cake(self): print(' self. Kongfu ') class Prentice(School, Master): Def __init__(self): self. Kongfu = ' 'def make_cake(self): Self.__init__() print(f' kongfu ') def make_master_cake(self): Master.__init__(self) Master.make_cake(self) def make_school_cake(self): School.__init__(self) School. Make_cake (self) # Class Tusun(Prentice): pass xiaoqiu = Tusun() xiaoqiu.make_cake() xiaoqiu.make_school_cake() xiaoqiu.make_master_cake()Copy the code
Super () calls the superclass method
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Print (f' use {self. Kongfu} to make kongfu ') class School(Master): def __init__(self): Self. kongfu = 'def make_cake(self): self.kongfu =' def make_cake(self): Print (f' use {self. Kongfu} to make kongfu ') # 2.1 # super(School, self).__init__() # super(School, self) Self).make_cake() # method 2.2 super().__init__() super().make_cake() class Prentice(School): def __init__(self): Self. kongfu = 'def make_cake(self): self.kongfu =' def make_cake(self): Def make_master_cake(self): self.__init__() print(f' use {self.kongfu} to make kongfu ') def make_master_cake(self): Master.__init__(self) Master.make_cake(self) def make_school_cake(self): School.__init__(self) School. Make_cake (self) # def make_old_cake(self): If the parent class name changes, # master.__init__ (self) # master.make_cake (self) # school.__init__ (self) # school.make_cake (self) # school.make_cake (self) # Super () # method 2.1 super(current class name, self). Function () # super(Prentice, self).__init__() # super(Prentice, self).make_cake() # method 2.2 super(). Function () super().__init__() super().make_cake() daqiu = Prentice() daqiu.make_old_cake()Copy the code
Note: Using super() automatically looks up the parent class. The call order follows the order of the __mro__ class attributes. It is suitable for single inheritance.
Viii. Private permissions
8.1 Defining private properties and methods
In Python, you can set private permissions for instance properties and methods, meaning that an instance property or instance method does not inherit to subclasses.
Story: Daqiu does not want to inherit his own money (2 million yuan) to his apprentice while passing on the technology, so it is necessary to set the private permission for the instance property of money.
To set private permissions, add two underscores __ before the property name and method name.
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Print (f' use {self.kongfu} to make kongfu ') class School(object): def __init__(self): Self. Kongfu = 'def make_cake(self): print(' self. Kongfu ') class Prentice(School, Master): Def __init__(self): self. Kongfu = 'kongfu' print(self.kongfu) print(self.__money) def make_cake(self): Self.__init__() print(f' kongfu ') def make_master_cake(self): Master.__init__(self) Master.make_cake(self) def make_school_cake(self): School.__init__(self) School. Make_cake (self) # Class Tusun(Prentice): Pass daqiu = Prentice() # Object cannot access private attributes and private methods # print(daqiu.__money) # daqiu.__info_print() xiaoqiu = Tusun() # # print(xiaoqiu.__money) # print(xiaoqiu.__info_print)Copy the code
Note: Private properties and methods can only be accessed and modified within the class.
8.2 Obtaining and modifying private Attribute values
In Python, the general function name get_xx is defined to get private attributes, and set_xx is defined to modify private attribute values.
Class Master(object): def __init__(self): self.kongfu = 'kongfu' def make_cake(self): Print (f' use {self.kongfu} to make kongfu ') class School(object): def __init__(self): Self. Kongfu = 'def make_cake(self): print(' self. Kongfu ') class Prentice(School, Master): Def get_money(self): self. Kongfu = 'self. Def set_money(self): return self.__money (self): return self. print(self.kongfu) print(self.__money) def make_cake(self): Self.__init__() print(f' kongfu ') def make_master_cake(self): Master.__init__(self) Master.make_cake(self) def make_school_cake(self): School.__init__(self) School. Make_cake (self) # Class Tusun(Prentice): Print (xiaoqio.get_money ()) # print(xiaoqio.get_money ()) # Xiaoqiu. Set_money () print(xiaoqiu. Get_money ())Copy the code
9. To summarize
-
Characteristics of inheritance
- A subclass has all the attributes and methods of its parent class by default
- Subclasses override methods and attributes of the same name as their parent class
- Subclasses invoke methods and attributes of the same name as their parent class
-
The super() method quickly calls the superclass method
-
Private permission
- Properties and methods that cannot be inherited to subclasses require private permissions
- grammar
Class class name (): # private attribute __ Attribute name = value # private method def __ Function name (self): codeCopy the code
I am Bai Youbai I, a program yuan who likes to share knowledge ❤️ if you have no contact with programming this blog, you can follow my public account: Bai Youbai Learn Python