Object-Oriented Inheritance and Derivation

Posted by Harsh on Sat, 11 May 2019 14:15:06 +0200

1. inheritance

1.1 Concept of Inheritance

Inheritance refers to the relationship between classes, what is the relationship between classes, and one of the functions of inheritance is to solve the problem of code reuse.
Inheritance is a way to create new classes. In Python, a newly created class can inherit one or more parent classes, which can become a base class or a superclass, and a newly created class can become a derived class or a subclass.

Class Inheritance Classification in 1.2 python: Single Inheritance and Multiple Inheritance

class ParentClass1: #Defining parent class
    pass

class ParentClass2: #Defining parent class
    pass

class SubClass1(ParentClass1): #Single inheritance, the base class is ParentClass1, and the derived class is SubClass
    pass

class SubClass2(ParentClass1,ParentClass2): #python supports multiple inheritances, separating multiple inherited classes with commas
    pass

1.3 View Inheritance

"__base__View only the first parent class inherited from left to right
__bases__View all inherited parent classes"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class ParentClass1: #Defining parent class
    pass

class ParentClass2: #Defining parent class
    pass

class SubClass1(ParentClass1): #Single inheritance, the base class is ParentClass1, and the derived class is SubClass
    pass

class SubClass2(ParentClass1,ParentClass2): #python supports multiple inheritances, separating multiple inherited classes with commas
    pass
print(SubClass1.__base__)
print(SubClass2.__base__)
print(SubClass1.__bases__)
print(SubClass2.__bases__)

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
<class '__main__.ParentClass1'>
<class '__main__.ParentClass1'>
(<class '__main__.ParentClass1'>,)
(<class '__main__.ParentClass1'>, <class '__main__.ParentClass2'>)

Process finished with exit code 0

1.4 Classical Classes and New Classes

1.Only in Python2 There are new and classical classes in China. Python3 All of them are new-style classes.
2.stay Python2 If there is no explicit inheritance object Class, that class and its subclasses are classical classes
3.stay Python2 If explicit inheritance object Class, that class and its subclasses are new-style classes
4.stay Python3 Chinese, whether inherited or not object,All default inheritance object,Namely Python3 All classes are new
5.stay Python3 If no base class is specified, inheritance is defaulted object Class, object All Python Class base class
print(ParentClass1.__bases__)
print(ParentClass2.__bases__)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
(<class 'object'>,)
(<class 'object'>,)

Process finished with exit code 0

2. Abstraction and Inheritance

Abstraction: Extraction of similar parts, mainly for categorization

Inheritance: Based on abstract results, it is realized by programming language, so it is necessary to abstract things or logic first, and then express abstract structure by inheritance.

3. Inheritance and Reuse

In development, if class A and class B have many identical codes, it can be achieved through inheritance to improve code reuse.
B can inherit all attributes of A (including data attributes and function attributes) and B can inherit all attributes of A.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Hero:
    def __init__(self,nickname,aggressivity,life_value):
        self.nickname=nickname
        self.aggressivity=aggressivity
        self.life_value=life_value

    def attack(self,enemy):
        enemy.life_value-=self.aggressivity
class Garen(Hero):
    pass

class Riven(Hero):
    pass

g1=Garen('Grass cluster',100,300)
r1=Riven('Lai Wen Wen',57,200)

print(g1.life_value) #Results: 300
r1.attack(g1)
print(g1.life_value) #Results: 243

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
300
243

Process finished with exit code 0
"Create a new class with the existing classes, so that you can reuse some of the code in the existing software, greatly saving the programming workload. This is software reuse. It can not only reuse its own classes, but also inherit classes written by others, and also inherit standard libraries."

4. Property Search and self Deep Understanding

Whether it's data or function attributes in a class, they are first searched in their own class, not in their own class, and then in their parent class.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Foo:
    def f1(self):
        print('Foo.f1')

    def f2(self):
        print('Foo.f2')
        self.f1()

class Bar(Foo):
    def f1(self):
        print('Bar.f1')

b=Bar()
b.f2()

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Foo.f2
Bar.f1

Process finished with exit code 0

Topics: Python Programming