Inheritance and polymorphism in object-oriented programming

1, Inheritance (important)

1. What is inheritance?

Inheritance is a way to create a new class. The new class is called a child class or parent class, and the inherited class is called a parent class or base class.

 

 1 class ParentClass1:  # Define parent class
 2     pass
 3 
 4 
 5 class ParentClass2:  # Define parent class
 6     pass
 7 
 8 
 9 class SubClass1(ParentClass1):  # Single inheritance, base class is ParentClass1,Derived classes are SubClass
10     pass
11 
12 
13 class SubClass2(ParentClass1, ParentClass2):  # python Support multiple inheritance, and separate multiple inherited classes with commas
14     pass
15 
16 
17 print(SubClass1.__bases__)  # View all inherited parent classes
18 print(SubClass2.__bases__)  # View all inherited parent classes

 

2. Why use inheritance?

Class solves the problem of code redundancy between objects

Inheritance solves the problem of code redundancy between classes

3. How to use inheritance?

New class: the children and grandchildren of the object class are new classes

Classic class: children and grandchildren that do not inherit the object class are classic classes

1. Only in python2 can there be new classes and classic classes. In python3, all of them are new classes

2. In python2, classes that do not explicitly inherit the object class and their subclasses are classic classes

3. In python2, it is explicitly declared that the class inheriting object and its subclasses are new classes

4. In Python 3, object is inherited by default regardless of whether it is inherited or not, that is, all classes in Python 3 are new classes

2, Class inheritance

Inheritance establishes the relationship between derived classes and base classes. It is a 'yes' relationship. For example, white horses are horses and people are animals.

When there are many same functions between classes, it is better to extract these common functions into a base class. For example, teachers are people and students are people.

 1 # Class inheritance
 2 # Parent public class
 3 class People:
 4     school = 'SH'
 5 
 6     def __init__(self, name, age, gender):
 7         self.name = name
 8         self.age = age
 9         self.gender = gender
10 
11 
12 class Student(People):
13     def __init__(self, name, age, gender, course=None):
14         if course is None:
15             course = []
16         People.__init__(self, name, age, gender)
17         self.course = course
18 
19     def choose_course(self, course):
20         self.course.append(course)
21         print('%s Successful course selection %s' % (self.name, self.course))
22 
23 
24 class Teacher(People):
25     def __init__(self, name, age, gender, level):
26         self.level = level
27         People.__init__(self, name, age, gender)
28 
29     def score(self, stu_obj, score):
30         stu_obj.score = score
31         print('%s to%s Yes%s branch' % (self.name, stu_obj.name, score))
32 
33 
34 stu1 = Student('jason', 18, 'male')
35 tea = Teacher('jack', 18, 'female', 'primary')
36 # print(Student.choose_course(stu1, 'python'))
37 stu1.choose_course('python')
38 Teacher.score(tea, stu1, 90)
39 print(stu1.__dict__)

3, Attribute search under single inheritance

The order of single attribute search is to find the object in the object first. If it is not found, then find it in the class that generates the object. If it is not found, then find it in the parent class.

 1 class Foo:
 2     def f1(self):  # _Foo__f1()
 3         print('Foo.f1')
 4 
 5     def f2(self):
 6         #
 7         print('Foo.f2')
 8         self.f1()  # _Foo__f1()
 9 
10 
11 class Bar(Foo):
12     def f1(self):  # # _Bar__f1()
13         print('Bar.f1')
14 
15 
16 obj = Bar()  # {}
17 obj.f2()

4, Multi attribute lookup

New type: query according to limited breadth, as shown in the figure

Classic class: query by depth first

 1 class A(object):
 2     def test(self):
 3         print('from A')
 4 
 5 
 6 class B(A):
 7     def test(self):
 8         print('from B')
 9 
10     pass
11 
12 
13 class C(A):
14     def test(self):
15         print('from C')
16 
17 
18 class D(B):
19     def test(self):
20         print('from D')
21     pass
22 
23 
24 class E(C):
25     def test(self):
26         print('from E')
27 
28 
29 class F(D, E):
30     pass
31 
32 
33 f1 = F()
34 f1.test()
35 print(F.mro())

5, super() and mro list

 1 class Student:
 2     school = 'SH'
 3 
 4     def __init__(self, name, age, gender):
 5         self.name = name
 6         self.age = age
 7         self.gender = gender
 8 
 9 
10 class Teacher(Student):
11     def __init__(self, name, age, gender, level):
12         self.level = level
13         super().__init__(name, age, gender)  # super Use of
14 
15     def score(self, stu_obj, score):
16         stu_obj.score = score
17         print('%s to%s Yes%s branch' % (self.name, stu_obj.name, score))
18 
19 
20 stu = Student('jason', 18, 'male')
21 tea = Teacher('jack', 18, 'female', 'primary')
22 tea.score(stu, 80)
23 
24 # mro List exercise 1
25 class A:
26     def test(self):
27         print('from A.test')
28         super().test()
29 
30 
31 class B:
32     def test(self):
33         print('from B')
34 
35 
36 class C(A, B):
37     pass
38 
39 
40 c = C()
41 c.test()
42 
43 
44 
45 class B:
46     def test(self):
47         print('B---->test')
48 
49     def aaa(self):
50         print('B---->aaa')
51 
52 
53 class A:
54     def test(self):
55         print('A---->test')
56         # super().aaa()
57 
58 
59 class C(A, B):
60     def aaa(self):
61         print('C----->aaa')
62 
63 
64 c = A()
65 c.test()  # Print results:A---->test
66 print(A.mro())

6, Polymorphism and polymorphism

class Animal:

    def eat(self):
        print('%s eat' % self.name)

    def drink(self):
        print('%s drink' % self.name)

    def shit(self):
        print('%s PULL' % self.name)

    def pee(self):
        print('%s Pee' % self.name)


class Dog(Animal):
    def __init__(self, name):
        self.name = name
        self.bread = 'Dog'

    def Call(self):
        print('Woof, woof')


class Cat(Animal):
    def __init__(self, name):
        self.name = name
        self.bread = 'cat'

    def Call(self):
        print('cat ')


# c1 = Dog('Little white dog')
# c1.eat()
# c1.Call()
c2 = Cat('Little white cat')
c2.pee()
c2.Call()

 

Posted by piet on Mon, 06 Dec 2021 13:41:31 -0800