python foundation - object oriented 3 - inheritance, super keyword, polymorphism

inherit
Concept:
Because different classes may have many common characteristics and behaviors, they have the same characteristics or behaviors defined in one class. These same characteristics and behaviors can be shared by other classes when defining other classes, which is inheritance, avoiding multiple definitions, simplifying operations, and making the system better maintained.
The inherited is called the parent class (superclass), and the inherited is called the child class.
Inheritance belongs to the relationship between classes.
A child class can inherit the methods and properties exposed by the parent class. Private methods and properties will not be inherited.

class RichMan(object):

    def __init__(self):
        self.money = 666666
        self.company = "ideal"
        self.__assistant = "Xiao Hao"

    def say(self):
        print("I'm rich, rich and willful")
        
# Indicates that the Son class inherits the RichMan class
# All public methods and properties in the RichMan class are inherited by the Son class
class Son(RichMan):
	pass
	
if __name__ == '__main__':
    # r = RichMan()
    # print(r.company)
    # print(r.money)
    # r.say()

    s = Son()
    print(s.money)
    print(s.company)
    s.say()
    
    #print(s.__assistant)       #If this statement is executed, an error will be reported

Overloading and rewriting
Overload of method:
The function name is the same, the number of function parameters and the type are different. When we call, the corresponding function will be executed automatically.
eg:
For C language
public int add(int x, int y){
return x+y }
public double add(double x,double y){
return x+y }
public int add(int x, int y,int z){
return x+y+z }
For strong data type languages, different functions are called according to the type and number of parameters passed
but
python:
def add(x,y):
return x+y
def add(x,y,z):
return x+y+z
For weakly typed languages, no matter what you pass, only the last function will be executed, so python has no overload.
Same as mentioned in blog object-oriented 2, explained here

Override of method:
The inherited methods and properties can't meet our needs. At this time, we can re define them in the subclass (the method name should be consistent). When calling, the methods defined in the subclass will be executed and the parent class will be overwritten.
Override the say method in the son subclass:

class Son(RichMan):

    def say(self):
        print("I don't like money")

Call s.say() and the rewritten will be displayed.

Multiple inheritance
python can inherit more. Java can only inherit by itself (A inherits B, and B cannot inherit others).

class RichMan(object):

    def __init__(self):
        self.money = 666666
        self.company = "ideal"
        self.__assistant = "Xiao Hao"

    def say(self):
        print("I'm rich, rich and willful")

class grandfather(object):
    def tell(self):
        print("I have a villa.")

class Son(RichMan,grandfather):    #Multiple inheritance
    pass

if __name__ == '__main__':

    s = Son()
    s.say()
    s.tell()


When two inherited classes have the same method, when they are called, the inheritance level is deep. But when subclass is rewritten, it must be defined by subclass.
For example:
class grandfather(object):
def say(self):
print("I have a villa")
The same say method as RichMan.

super keyword
If you want to call the method of the parent class in the subclass,
For those that do not exist in a subclass, you can use self, and those that cannot be found in a subclass will be found in the parent class.
Use super for everything.
super is only used in subclasses.

class RichMan(object):

    def __init__(self,money,company,assistant):
        self.money = money
        self.company = company
        self.__assistant = assistant
        
class Son(RichMan):

    def __init__(self,name):
        #super points to the parent class
        super().__init__(2000,"company","secretary")  #To call the initialization of the parent class, you need three parameters, which can be passed directly to.
        self.__name = name

    def get_name(self):
        return self.__name
    def set_name(self, name):
        self.__name = name

    def say(self):
        print("I don't like money")


if __name__ == '__main__':
 
    s = Son("timi")
    print(s.get_name())
    print(s.company)


Polymorphism (no emphasis)
On the basis of inheritance, the parent class refers to the phenomenon of subclass instance, and the parent class can call the subclass.
Solve the parent class calling the child class.
Important: weakly typed languages (no emphasis on data types) inherently support polymorphism.

Java (strong data type) example:
List ls = new ArrayList(); / / list is the parent class of ArrayList
The type List of ls is the type of the parent class
ls has all ArrayList methods

Published 13 original articles, won praise 1, visited 294
Private letter follow

Tags: Python Java C

Posted on Fri, 13 Mar 2020 05:01:09 -0400 by lawnmowerman