One way to create a new class based on an existing class is to let one class directly inherit properties and methods from another class, so as to reduce the writing of repeated code. The class that provides inheritance information is called parent class, also called superclass or base class; The inheritance information we get is called subclass, also known as derived class or derived class. In addition to inheriting the attributes and methods provided by the parent class, subclasses can also define their own unique attributes and methods. Therefore, subclasses have more abilities than the parent class. In actual development, we often replace a parent object with a subclass object, which is a common behavior in object-oriented programming. The corresponding principle is called [Richter replacement principle]( https://zh.wikipedia.org/wiki/%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8D%A2%E5%8E%9F%E5%88%99). Let's take a look at an example of inheritance.
class Person(object): """people""" def __init__(self, name, age): self._name = name self._age = age @property def name(self): return self._name @property def age(self): return self._age @age.setter def age(self, age): self._age = age def play(self): print('%s Playing happily.' % self._name) def watch_av(self): if self._age >= 18: print('%s Watching a love action movie.' % self._name) else: print('%s Can only watch bear haunt.' % self._name) class Student(Person): """student""" def __init__(self, name, age, grade): super().__init__(name, age) self._grade = grade @property def grade(self): return self._grade @grade.setter def grade(self, grade): self._grade = grade def study(self, course): print('%s of%s I am learning%s.' % (self._grade, self._name, course)) class Teacher(Person): """teacher""" def __init__(self, name, age, title): super().__init__(name, age) self._title = title @property def title(self): return self._title @title.setter def title(self, title): self._title = title def teach(self, course): print('%s%s Talking%s.' % (self._name, self._title, course)) def main(): stu = Student('Da Chui Wang', 15, 'Junior three') stu.study('mathematics') stu.watch_av() t = Teacher('Luo Hao', 38, 'Brick house') t.teach('Python Programming') t.watch_av() if __name__ == '__main__': main()
After a subclass inherits the method of the parent class, it can give a new implementation version of the existing method of the parent class. This action is called method override. Through method rewriting, we can make the same behavior of the parent class have different implementation versions in the subclass. When we call the method rewritten by the subclass, different subclass objects will show different behaviors. This is poly morphism.
from abc import ABCMeta, abstractmethod class Pet(object, metaclass=ABCMeta): """Pets""" def __init__(self, nickname): self._nickname = nickname @abstractmethod def make_voice(self): """Make a sound""" pass class Dog(Pet): """dog""" def make_voice(self): print('%s: Woof, woof...' % self._nickname) class Cat(Pet): """cat""" def make_voice(self): print('%s: Meow...Meow...' % self._nickname) def main(): pets = [Dog('Wangcai'), Cat('Katie'), Dog('chinese rhubarb')] for pet in pets: pet.make_voice() if __name__ == '__main__': main()
In the above code, we treat the Pet class as an abstract class. The so-called abstract class is a class that cannot create objects. This kind exists specifically for other classes to inherit it. Python does not support abstract classes like Java or C# in terms of syntax, but we can achieve the effect of abstract classes through ABCMeta metaclass and abstractmethod wrapper of abc module. If there are abstract methods in a class, the class cannot be instantiated (create objects). In the above code, the two subclasses of Dog and Cat are respectively used to make in the Pet class_ The voice abstraction method is rewritten and different versions are implemented. When we call this method in the main function, this method shows polymorphic behavior (the same way does different things).