One of the characteristics of object-oriented: Inheritance
What is inheritance?
Inheritance is a way to create a new class. The newly created class can inherit one or more parent classes (python supports multiple inheritance). The parent class can also be called base class or super class, and the newly created class is called derived class or subclass.
Subclasses inherit the properties of the parent class, and can also use the methods of the parent class to solve the problem of code redundancy
Class inheritance in python is divided into single inheritance and multi inheritance
# Single inheritance # Taking the student course selection system as an example # Parent class, public class class People(): school = 'SH' def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender # Student class class Student(People): def __init__(self, name, age, gender, course=None): if course is None: course = [] People.__init__(self, name, age, gender) self.courses = course def choose_course(self, course): self.courses.append(course) print('%s Successful course selection %s' % (self.name, self.courses)) stu = Student('tony', 19, 'male') # teacher class class Teacher(People): def __init__(self, name, age, gender, level): self.level = level People.__init__(self, name, age, gender) def score(self, stu_obj, score): stu_obj.score = score # Grade students print('%s to%s Yes%s branch' % (self.name, stu_obj.name, score)) tea = Teacher('lucy', 28, 'male', 10) print(tea.name) print(tea.level)
Attribute search under single inheritance
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') obj = Bar() # {} obj.f2() # practice class Foo: def __f1(self): # _Foo__f1() print('Foo.f1') def f2(self): # print('Foo.f2') self.__f1() # _Foo__f1() class Bar(Foo): def __f1(self): # # _Bar__f1() print('Bar.f1') obj = Bar() # {} obj.f2()
Attribute search under multiple inheritance
# New category: query by breadth first # Classic class: query by depth first # Python 3 is full of new classes class A(object): def test(self): print('from A') class B(A): # def test(self): # print('from B') pass class C(A): # def test(self): # print('from C') pass class D(B): # def test(self): # print('from D') pass class E(C): # def test(self): # print('from E') pass class F(D, E): # def test(self): # print('from F') pass f1 = F() f1.test()
super() and mro list
class People(): school = 'SH' def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender class Teacher(People): def __init__(self, name, age, gender, level): self.level = level super().__init__(name, age, gender) # Use of super # mro list exercise 1 class A: def test(self): print('from A.test') super().test() class B: def test(self): print('from B') class C(A, B): pass c = C() c.test() # mro list exercise 2 class B: def test(self): print('B---->test') def aaa(self): print('B---->aaa') class A: def test(self): print('A---->test') super().aaa() class C(A, B): def aaa(self): print('C----->aaa') c = A() # c.test() # Print results: print(A.mro())
abstract class
Abstract class is a special class. Its particularity is that it can only be inherited and cannot be instantiated
Why abstract classes
If a class extracts the same content from a pile of objects, then an abstract class extracts the same content from a pile of classes, including data attributes and function attributes
#Everything is a document import abc #Using abc module to implement abstract class class All_file(metaclass=abc.ABCMeta): all_type='file' @abc.abstractmethod #Define abstract methods without implementing functions def read(self): 'Subclass must define read function' pass @abc.abstractmethod #Define abstract methods without implementing functions def write(self): 'Subclass must define write function' pass # class Txt(All_file): # pass # # t1=Txt() #An error is reported. The subclass does not define an abstract method class Txt(All_file): #Subclasses inherit abstract classes, but read and write methods must be defined def read(self): print('Reading method of text data') def write(self): print('Reading method of text data') class Sata(All_file): #Subclasses inherit abstract classes, but read and write methods must be defined def read(self): print('Reading method of hard disk data') def write(self): print('Reading method of hard disk data') class Process(All_file): #Subclasses inherit abstract classes, but read and write methods must be defined def read(self): print('Reading method of process data') def write(self): print('Reading method of process data') txt1=Txt() sata1=Sata() process1=Process() #In this way, everyone is normalized, that is, the idea that everything is a document txt1.read() sata1.write() process1.read() print(txt1.all_type) print(sata1.all_type) print(process1.all_type)
Two of the three characteristics of object-oriented: polymorphism
Polymorphism means that a kind of thing has many forms
For example, animals have many forms: people, dogs and pigs
import abc class Animal(metaclass=abc.ABCMeta): #The same thing: Animals @abc.abstractmethod def talk(self): pass class People(Animal): #One of the forms of animals: man def talk(self): print('say hello') class Dog(Animal): #Animal form 2: dog def talk(self): print('say wangwang') class Pig(Animal): #Animal form 3: Pig def talk(self): print('say aoao')
Polymorphism
Polymorphism refers to the use of instances regardless of instance type
In object-oriented methods, polymorphism is generally expressed as follows:
Send the same message to different objects (!!! obj.func(): call obj's method func, also known as sending a message func to obj). Different objects will have different behaviors (i.e. methods) when receiving.
That is, each object can respond to a common message in its own way. The so-called message refers to calling functions. Different behaviors refer to different implementations, that is, executing different functions.
For example: teacher. Class bell rings (), student. Class bell rings (), the teacher performs off-duty operation, and the student performs after-school operation. Although the two messages are the same, the execution effect is different
peo=People() dog=Dog() pig=Pig() #peo, dog and pig are animals. As long as they are animals, there must be a talk method #So we can use them directly without considering the specific types of the three peo.talk() dog.talk() pig.talk() #Further, we can define a unified interface to use def func(obj): obj.talk()