Popular explanation lets you understand the object-oriented inheritance in Python in seconds

Object Oriented Programming, one of the main functions of object-oriented programming is "inheritance". Inheritance refers to the ability to use all the functions of an existing class and extend them without having to rewrite the original class. Inheritance, in fact, is that I wrote a father class and a son class. The father has money, but the son has no money. So the son decided to inherit the father and call the father's money (variables and methods of the father class). Inherit a class, basically using the following five methods.

1. Call the parent property method directly

Father has money, son has no money, so son uses father's money

class Father():
    def __init__(self):
        self.money= 1000 
    def action(self):
        print('Call the method of the parent class')
 
class Son(Father):
    pass
 
son=Son()     # Subclass Son inherits all the properties and methods of the parent
son.action()  # Call parent property
//Output: methods calling the parent class
son.money     # Call parent property
//Output: 1000

2. Force call to parent private property method

Dad said, you son, always use my money, I decided to hide private money. Son try super() to get your private house money, but here we need to pay attention to super() to call the private property method of the parent class forcibly, that is, rewrite the method. Private variables cannot be inherited by super, nor can they access the variables of the private property method in the parent class, that is, children cannot get private house money.

class Father():
    __money  = 1000 #Private variable cannot inherit
    
    def __action(self):  # Private method of parent class
        money = 1000
        print('Call the method of the parent class')
 
class Son(Father):
        
    def action(self):
        super()._Father__action()
        print(money) 
        
son=Son()
son.action()
​

Call the method of the parent class

name 'money' is not defined

3. Override parent property method

All of a sudden, my son had money, so he decided not to use his father's money, but to rewrite the method of parent property.

class Father():
    def __init__(self):
        self.money = 0
    
    def action(self):
        print('Call the method of the parent class')
 
class Son(Father):
    def __init__(self):
        self.money = 1000
      
    def action(self):
        print('Method of subclass overriding parent class')
 
son=Son()     # Subclass Son inherits all the properties and methods of the parent
son.action()  # The child class Son calls its own action method instead of the parent class's action method
son.money     # Own 1000

4. Call init method of parent class

If the father put the money in the "init", is it possible for the son to get his father's money? It's not a private variable, it's not a private house money, of course you can get it Let's see if we can get it without super.

class Father():
    def __init__(self):
        self.money = 1000
 
class Son(Father):
    def __init__(self):
        pass
    
son=Son()
print(son.money)
​
# Error reported: 'Son' object has no attribute 'money'
​

It's just like taking money if you don't need super. I look down on your father and me.

class Father():
    def __init__(self):
        self.money = 1000
 
class Son(Father):
    def __init__(self):
        super().__init__()
        #You can also use family. Init (self) the self in this must be added (the above two are the same)
        
        
son=Son()
print(son.money)
​
1000

5. Inherit parameters in parent class initialization

Sometimes, my father needs to earn money and spend money, which is the parameter in our initialization process. My son is very curious and decides to see how much money my father still has in his pocket. Let's kill ear'n'money and spend'n'money

class Father():
    def __init__(self):
        self.earn_money=1000
        self.spend_money= -500
 
class Son(Father):
    def __init__(self):
        super().__init__()
        #You can also use family. Init (self) the self in this must be added
        
    def add(self):
        return self.earn_money+self.spend_money
        
        
son=Son()
print(son.add())
​
500

The son found that his father didn't have enough money, so he secretly took some money.

class Father():
    def __init__(self,a,b):
        self.earn_money = a
        self.spend_money= b
    def add(self):
        return self.a + self.b
 
#Call parent class to initialize parameter a,b and add extra parameter c
class Son(Father):
    def __init__(self,a,b,c=1000):  # c fixed value
        Father.__init__(self,a,b)  
        self.son_money = c
        
    def add(self):
        return self.earn_money+self.spend_money + self.son_money
        
   
        
son=Son(1000,-500)   # So c can be expressed without display
print(son.add())     # Call the subclass add function
​
1500

The above basically covers the inheritance of Python classes, the basic content of calling the properties and methods of the parent class. You can write some cases by yourself to deepen your understanding.

Source network, only for learning, if there is infringement, contact delete.

I have collected high-quality technical articles and experience summary in my official account [Python circles].

Don't panic. I have a set of learning materials, including 40 + E-books, 600 + teaching videos, involving Python foundation, reptile, framework, data analysis, machine learning, etc. I'm not afraid you won't learn! There are also learning exchange groups, learning and progress together~

file

Tags: Programming Python Attribute network

Posted on Mon, 06 Apr 2020 05:36:42 -0400 by diesel