Python foundation 6 -- Class 3 (encapsulation, inheritance and polymorphism of classes)

catalogue

1, Encapsulation

2, Class inheritance

3, Polymorphism

The focus of python programming is object-oriented programming, and the core methods of object-oriented programming are encapsulation, inheritance and polymorphism.

1, Encapsulation

First, I will briefly introduce the encapsulation. My personal understanding is to seal the data and methods, and then install them into a class. Users do not pay attention to the implementation logic inside. They only need to pass qualified parameters and call the corresponding methods to get the results. A class is the encapsulation of multiple related function functions.

Write a simple example:

class Jisuan():     #Define a calculated class
    def __init__(self,x,y):   #Set attribute x, y
        self.x = x     #Define attribute x
        self.y = y     #Define attribute y
    def add2(self):    #Define an addition method 
        print(self.x+self.y)  #Prints the result of the addition of x and y
    def subtract2(self): #Define a method of multiplication
        print(self.x*self.y)  #Print the result of multiplication

Atom =Jisuan(1,2)    #Create instance Atom
Atom.add2()    #Call the addition method, and the print result is: 3
Atom.subtract2()   #Call the multiplication method, and the print result is: 2

The above example can be understood as a package. I don't need to close the internal design. I just need to pass in two numbers. If I want them to add, I can adjust the method of adding, and if I want them to multiply, I can adjust the method of multiplying

2, Class inheritance

1. Inherit parent class

Class inheritance can be seen from the literal meaning. Function is inheritance. For example, in real life, a father has a car, a house and 100W. These things can also be inherited as sons. When we define sons, we can directly inherit these things from the father.

Take an example:

class Father():    #Define a Father class
    def car(self):   #How to define a car
        print("I have a car")  #I have a car
    def house(self):  #Define a housr method
        print("I have a house")  #I have a suite
    def money(self):   #How to define money
        print("I have a million")  #I have a million

class Son(Father):   #Define a son's class, and then inherit the things of the Father class
    def marry(self): #Define a way. I'm married
        print("I have a daughter-in-law")  #I have a daughter-in-law

son1 = Son()   #Create instance son1
son1.money()   #Call the method of money, and the print result is: I have 1 million
son1.marry()   #Call Mary's method and the print result is: I have a daughter-in-law

As can be seen from the above example, there is no money method under Son's class, but because it inherits the Father class, you can directly use the functions under the Father class.

2. Replication of methods

The father plans to donate 20W and only leave 80W for his son, so the father has 100W, but the son has only 80W. How should the method be called at this time?

In this way, you can copy a new method. The copied method makes the method name of the parent class and the method name of the child class the same. Then, when the child class calls the method, the method of the parent class will not be called, but the method newly written by the child class will be called.

class Father():    #Define a Father class
    def car(self):   #How to define a car
        print("I have a car")  #I have a car
    def house(self):  #Define a housr method
        print("I have a house")  #I have a suite
    def money(self):   #How to define money
        print("I have a million")  #I have a million

class Son(Father):   #Define a son's class, and then inherit the things of the Father class
    def marry(self): #Define a method, Mary
        print("I have a daughter-in-law")  #I have a daughter-in-law
    def money(self):  #Define a method money 
        print("I only have 800000") #I only have 800000
 
son1 = Son()   #Create instance son1
father1 = Father()  #Create instance father1
son1.money()   #son1 calls the method of money, and the print result is: I have only 800000
father1.money()   #father1 calls the method of money, and the print result is: I have 100W

For example, when Son calls the money method, he should call the money method of the inherited parent class, but the requirement requires that Son's money is his own money and cannot use the parent class, so a new money method is written in Son's class, so when Son calls the money method, he will call the money method in Son's class, But when Father calls the money method, he still calls the money in his own function.

3. Inherit multiple classes

A subclass can also inherit multiple parent classes. Just add the parent class to be inherited in () after the subclass, separated by commas.

class Father():    #Define a Father class
    def car(self):   #How to define a car
        print("I have a car")  #I have a car
    def house(self):  #Define a housr method
        print("I have a house")  #I have a suite
    def money(self):   #How to define money
        print("I have a million")  #I have a million

class Mother():   #Define a Mother class
    def house(self):   #Define a method of house
        print("I have a big villa")  #I have a big villa
    def company(self):  #Define a company method
        print("I have a company")  #I have a company

class Son(Mother,Father):   #Define a son's class, and then inherit the things of Mother and Father
    def marry(self): #Define a way. I'm married
        print("I have a daughter-in-law")  #I have a daughter-in-law
    def money(self):  #Define a method Money
        print("I only have 800000") #I only have 800000

son1 = Son()   #Create instance son1
son1.house()   #Call the house method and the print result is: I have a villa
son1.company()  #Call the company method of the parent class, and the print result is: I have a company

Like this example, I created a parent class of mother and inherited it to Son. At this time, Son can use the method in mother. However, if the two inherited parent classes have the same method, when the child class inherits, the parent class in () is in front, whose method will be called, such as the house method called above, and Father's is a house, Mother's is a big villa, but because mother is written in front during inheritance, Son calls mother's house method when calling.

3, Polymorphism

Polymorphism means that functions with different functions can use the same function name, so that functions with different contents can be called with one function name. It can be simply understood that functions with the same name in different classes implement different methods. The replication of the above method also belongs to a kind of function polymorphism, The money method implements different functions in different examples. You can see a simple example.

class Animal():   #Define an Animal class
    def talk(self):   #Define a talk method
        print("Can call")  #Printing will call

class Dog(Animal):   #Define a Dog class
    def talk(self):  #Define a talk method
        print("Will bark")  #Printing will bark

class Cat(Animal):   #Define a Cat class
    def talk(self):  #Define a talk method
        print("Can meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow")  #The printer will meow

animal = Animal()  #Instantiate class Animal
animal.talk()      #Call the talk method and the print result is: will call
dog = Dog()  #Instantiate class Dog
dog.talk()      #Call the talk method and the print result is: Barking
cat = Cat()  #Instantiation class Cat
cat.talk()      #Call the talk method and the print result is: meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow meow

For example, in this example, when calling the talk method of Dog and Cat classes, both Dog and Cat classes have their own talk method and the inherited Animal talk method. When calling, first find their own talk method. If not, then call the talk method of the parent class, which can be called polymorphism.

The official account is two-dimensional code. The content will be sent out synchronously, so we can pay attention to learning together.

                                                                                               

This is the official account of Zhang Gouzi's brother brother. He will share some of his usual work experience and watch it.

                        ​​​​​​​        ​​​​​​​        ​​​​​​​                                                        

Keywords: Python

Added by sKunKbad on Fri, 11 Feb 2022 00:56:23 +0200