[Python beginner] object oriented (Part 2)

1. Multiple inheritance

Python also has limited support for multiple inheritance forms. The multi inherited class definition shape is as follows:

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>

Note the order of the parent classes in parentheses. If the parent class has the same method name, but it is not specified when the child class is used, python searches from left to right, that is, if the method is not found in the child class, it searches from left to right to find whether the parent class contains the method.

Demo1 calls the parent class separately:
A subclass inherits from multiple parents at the same time, also known as diamond inheritance and diamond inheritance.  
When calling a parent class using the parent class name. init(self):

class Parent(object):
    def __init__(self, name):
        self.name = name
        print('parent Of init End called')


class Son1(Parent):
    def __init__(self, name, age):
        Parent.__init__(self, name)
        self.age = age
        print('Son1 Of init End called')


class Son2(Parent):
    def __init__(self, name, gender):
        Parent.__init__(self, name)
        self.gender = gender
        print('Son2 Of init End called')


class Grandson(Son1, Son2):
    def __init__(self, name, age, gender):
        Son1.__init__(self, name, age)  # Call the initialization method of the parent class separately
        Son2.__init__(self, name, gender)
        print('Grandson Of init End called')


gs = Grandson('grandson', 12, 'male')

Operation result:

init end of parent called
 init end of Son1 called
 init end of parent called
 init end of Son2 called
 Grandson's init end is called

  Demo2:

#!/usr/bin/python3
 
#Class definition
class people:
    #Define basic properties
    name = ''
    age = 0
    #Define private properties,Private properties cannot be accessed directly outside the class
    __weight = 0
    #Define construction method
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    def speak(self):
        print("%s say: I %d Year old." %(self.name,self.age))
 
#Single inheritance example
class student(people):
    grade = ''
    def __init__(self,n,a,w,g):
        #Calling the constructor of the parent class
        people.__init__(self,n,a,w)
        self.grade = g
    #Method of overriding parent class
    def speak(self):
        print("%s say: I %d Age, I'm reading %d grade"%(self.name,self.age,self.grade))
 
#Another class, preparation before multiple inheritance
class speaker():
    topic = ''
    name = ''
    def __init__(self,n,t):
        self.name = n
        self.topic = t
    def speak(self):
        print("My name is %s,I'm a speaker. The theme of my speech is %s"%(self.name,self.topic))
 
#multiple inheritance
class sample(speaker,student):
    a =''
    def __init__(self,n,a,w,g,t):
        student.__init__(self,n,a,w,g)
        speaker.__init__(self,n,t)
 
test = sample("Tim",25,80,4,"Python")
test.speak()   #The method name is the same. By default, the method of the parent class is called in parentheses

Operation result:

My name is Tim. I'm a speaker. My topic is Python

2. Method rewrite

If the function of your parent method cannot meet your needs, you can override the method of your parent in the child class. The example is as follows:

#!/usr/bin/python3
 
class Parent:        # Defining parent class
   def myMethod(self):
      print ('Call parent method')
 
class Child(Parent): # Defining subclasses
   def myMethod(self):
      print ('Call subclass method')
 
c = Child()          # Subclass instance
c.myMethod()         # Subclass call override method
super(Child,c).myMethod() #Call a method whose parent class has been overridden with a subclass object

Input results:

Call subclass method
 Call parent method

3. Class properties and methods

Private property of class

"Private" attrs: starts with two underscores, declaring that the property is private and cannot be used or directly accessed outside the class. Self. Private. Attrs when used in methods within a class.

Method of class

In the inner part of a class, def keyword is used to define a method. Unlike general function definitions, a class method must contain the parameter self, which is the first parameter. Self represents an instance of the class.

The name of self is not specified to be dead. You can also use this, but it's better to use self according to the Convention.

Private method of class

"Private method": two underscores start, declaring that the method is private and can only be called inside the class, not outside the class. self.__private_method s

Keywords: Python

Added by biopv on Thu, 05 Mar 2020 13:41:32 +0200