Python note 17 - object oriented - inheritance

Posted by webhamster on Thu, 24 Feb 2022 12:34:57 +0100

Python notes

Object oriented - inheritance

I Introduction to inheritance

Inheritance Encyclopedia

Python object-oriented inheritance refers to the ownership relationship between multiple classes, that is, the subclass inherits all the properties and methods of the parent class by default.
In Python, all classes inherit the object class by default. The object class is the top-level class or base class; Other subclasses are called derived classes.
Examples

# Parent class
class A(object):
	def __init__(self):
		self.name='Jack'
	def info_p(self):
		print(self.name)
# Subclass
class B(A):
	pass

b1=B()
b1.info_p() # Jack

Note:

  • Classic or legacy class: a class that is not derived from any built-in type.
class Class name:
	code
  • New class
class Class name(object):
	code

II Single inheritance

Single inheritance: a subclass has only one parent.
Examples

# Basket class
class Basket(object):
	def __init__(self):
		self.teach='basketball'
	def train(self):
		print(f'teach: {self.teach}')

# Train class
class Train(Basket):
	pass
	
# create object
t1=Train()

# Object access instance properties
print(t1.teach) # basketball

# Object to call instance method
t1.train() # teach: basketball

III Multiple inheritance

Multiple inheritance: a class inherits multiple parent classes at the same time.

# Basket class
class Basket(object):
	def __init__(self):
		self.play='basketball'
	def train(self):
		print(f'train: {self.play}')

# Foot class
class Foot(object):
	def __init__(self):
		self.play='football'
	def train(self):
		print(f'train: {self.play}')


# Play class
class Play(Foot,Basket):
	pass
	
# create object
p1=Play()

# Object access instance properties
print(p1.play) # football

# Object to call instance method
p1.train() # train: football

Note: when a class inherits multiple classes, it inherits the properties and methods of the same name of the first parent class by default.

IV Subclasses override properties and methods with the same name as the parent class

When a subclass and a parent class have properties and methods with the same name, the properties and methods with the same name of the subclass are used by default.

# Basket class
class Basket(object):
    def __init__(self):
        self.play = 'basketball'

    def train(self):
        print(f'train: {self.play}')


# Foot class
class Foot(object):
    def __init__(self):
        self.play = 'football'

    def train(self):
        print(f'train: {self.play}')


# Play class
class Play(Foot,Basket):
	def __init__(self):
		self.play='badminton'
	def train(self):
		print(f'train: {self.play}')
# create object
p1 = Play()

# Object access instance properties
print(p1.play)  # badminton

# Object to call instance method
p1.train()  # train: badminton

V Subclasses call properties and methods with the same name as the parent class

# Basket class
class Basket(object):
    def __init__(self):
        self.play = 'basketball'

    def train(self):
        print(f'train: {self.play}')


# Foot class
class Foot(object):
    def __init__(self):
        self.play = 'football'

    def train(self):
        print(f'train: {self.play}')


# Play class
class Play(Foot,Basket):
    def __init__(self):
        self.play='badminton'
    def train(self):
        # If the properties and methods of the parent class are called first, the properties of the parent class will override the properties of the class. Therefore, before calling, call the initialization of the child class itself
        self.__init__()
        print(f'train: {self.play}')
    # To invoke the parent class method, in order to ensure that the properties of the parent class are called, the initialization of the parent class must be called before calling the method.
    def train_basket(self):
        Basket.__init__(self)
        Basket.train(self)
    def train_foot(self):
        Foot.__init__(self)
        Foot.train(self)

# create object
p = Play()

# Object to call instance method
p.train()  # train: badminton
p.train_basket()  # train: basketball
p.train_foot()  # train: football
p.train()  # train: badminton

Vi super() calls the parent class method

grammar

# 1.
super(Current class name,self).__init__()
super().function()
#2.
super().__init__()
super().function()

Examples

# Foot class
class Foot(object):
    def __init__(self):
        self.play = 'football'

    def train(self):
        print(f'train: {self.play}')


# Play class
class Play(Foot):
    def __init__(self):
        self.play='badminton'
    def train(self):
        # If the properties and methods of the parent class are called first, the properties of the parent class will override the properties of the class. Therefore, before calling, call the initialization of the child class itself
        self.__init__()
        print(f'train: {self.play}')

    def train_ball(self):
    	# 1.
        # super(Play, self).__init__()
        # super(Play, self).train()
        # 2.
        super().__init__()
        super().train()
# create object
p = Play()

# Object to call instance method
p.train_ball() # train: football

Note: using super() can automatically find the parent class. Call order follows__ mro__ The order of class properties. It is more suitable for single inheritance

VII Multilayer inheritance

Examples

# Basket class
class Basket(object):
    def __init__(self):
        self.play = 'basketball'

    def train(self):
        print(f'train: {self.play}')


# Foot class
class Foot(object):
    def __init__(self):
        self.play = 'football'

    def train(self):
        print(f'train: {self.play}')


# Play class
class Play(Foot,Basket):
    def __init__(self):
        self.play='badminton'
    def train(self):
        # If the properties and methods of the parent class are called first, the properties of the parent class will override the properties of the class. Therefore, before calling, call the initialization of the child class itself
        self.__init__()
        print(f'train: {self.play}')
    # To invoke the parent class method, in order to ensure that the properties of the parent class are called, the initialization of the parent class must be called before calling the method.
    def train_basket(self):
        Basket.__init__(self)
        Basket.train(self)
    def train_foot(self):
        Foot.__init__(self)
        Foot.train(self)

# Play1 class
class Play1(Play):
    pass

# create object
p1 = Play1()

# Object to call instance method
p1.train()  # train: badminton
p1.train_basket()  # train: basketball
p1.train_foot()  # train: football
p1.train()  # train: badminton

Note: if we first call the properties and methods of the parent class in the subclass, the parent class property will override the subclass attribute. Before calling, we call the initialization of the subclass and call the parent class method. In order to ensure that the attribute of the parent class is called, we must call the initialization of the parent class before calling the method.

VIII Private rights

Definitions and methods 1. Private

  • grammar
class Class name():
	# Private property
	__Attribute name=value
	# Private method
	def __Function name( self):
		code

**If private permission is set for instance property or instance method, subclasses cannot inherit
Set private permission: add two English underscores _ before the property name or method name.
**

Note: private properties and private methods can only be accessed and modified in classes

  • Examples
# Basket class
class Basket(object):
    def __init__(self):
        self.play = 'basketball'

    def train(self):
        print(f'train: {self.play}')


# Foot class
class Foot(object):
    def __init__(self):
        self.play = 'football'

    def train(self):
        print(f'train: {self.play}')


# Play class
class Play(Foot, Basket):
    def __init__(self):
        self.play = 'badminton'
        # Define private properties
        self.__drink = 'water'

    # Define private methods
    def __p_info(self):
        print(self.play)
        print(self.__drink)

    def train(self):
        # If the properties and methods of the parent class are called first, the properties of the parent class will override the properties of the class. Therefore, before calling, call the initialization of the child class itself
        self.__init__()
        print(f'train: {self.play}')

    # To invoke the parent class method, in order to ensure that the properties of the parent class are called, the initialization of the parent class must be called before calling the method.
    def train_basket(self):
        Basket.__init__(self)
        Basket.train(self)

    def train_foot(self):
        Foot.__init__(self)
        Foot.train(self)


# Play1 class
class Play1(Play):
    pass

p=Play()
# Object cannot access private properties and private methods
# print(p.__drink)
# p.__p_info()

p1=Play1()
# Subclasses cannot inherit private properties and methods of the parent class
# print(p1.__drink)
# p1.__p_info()

2. Get and modify private properties

The function name get is generally defined in Python_ XX is used to obtain private attributes and define set_xx is used to modify private properties.

# Basket class
class Basket(object):
    def __init__(self):
        self.play = 'basketball'

    def train(self):
        print(f'train: {self.play}')


# Foot class
class Foot(object):
    def __init__(self):
        self.play = 'football'

    def train(self):
        print(f'train: {self.play}')


# Play class
class Play(Foot, Basket):
    def __init__(self):
        self.play = 'badminton'
        # Define private properties
        self.__drink = 'water'

    # Define private methods
    def __p_info(self):
        print(self.play)
        print(self.__drink)

    # Get private properties
    def get_drink(self):
        return self.__drink

    # Modify private properties
    def set_drink(self):
        self.__drink='juice'

    def train(self):
        # If the properties and methods of the parent class are called first, the properties of the parent class will override the properties of the class. Therefore, before calling, call the initialization of the child class itself
        self.__init__()
        print(f'train: {self.play}')

    # To invoke the parent class method, in order to ensure that the properties of the parent class are called, the initialization of the parent class must be called before calling the method.
    def train_basket(self):
        Basket.__init__(self)
        Basket.train(self)

    def train_foot(self):
        Foot.__init__(self)
        Foot.train(self)


# Play1 class
class Play1(Play):
    pass

p=Play()

print(p.get_drink()) # water

p.set_drink()
print(p.get_drink()) # juice

p1=Play1()

print(p1.get_drink()) # water

p1.set_drink()
print(p1.get_drink()) # juice

I Miss You2.3
17
Levi_5

Topics: Python Pycharm