Day 20 OOP

day20思维导图

一 Inheritance继承

Inheritance in Python

Inheritance is a powerful feature in object oriented programming.

It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.

Python Inheritance Syntax

class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class

Derived class inherits features from the base class where new features can be added to it. This results in re-usability of code.

Add the init() Function

So far we have created a child class that inherits the properties and methods from its parent.

We want to add the init() function to the child class (instead of the pass keyword).

Note: The init() function is called automatically every time the class is being used to create a new object.

class Student(Person):
def __init__(self, fname, lname):
  #add properties etc.

When you add the init() function, the child class will no longer inherit the parent's init() function.

Note: The child's init() function overrides the inheritance of the parent's init() function

To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function:

class Student(Person):
def __init__(self, fname, lname):
  Person.__init__(self, fname, lname)

Use the super() Function

Python also has a super() function that will make the child class inherit all the methods and properties from its parent:

class Student(Person):
def __init__(self, fname, lname):
  super().__init__(fname, lname)

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.

Add Properties

class Student(Person):
def __init__(self, fname, lname):
  super().__init__(fname, lname)
  self.graduationyear = 2019
class Student(Person):
def __init__(self, fname, lname, year):
  super().__init__(fname, lname)
  self.graduationyear = year

x = Student("Mike", "Olsen", 2019)

Add Methods

class Student(Person):
def __init__(self, fname, lname, year):
  super().__init__(fname, lname)
  self.graduationyear = year

def welcome(self):
  print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

二 Multiple Inheritance 多继承

A class can be derived from more than one base class in Python. This is called multiple inheritance.

In multiple inheritance, the features of all the base classes are inherited into the derived class. The syntax for multiple inheritance is similar to single inheritance.

Method Resolution Order in Python(MRO)

Every class in Python is derived from the object class. It is the most base type in Python.

So technically, all other classes, either built-in or user-defined, are derived classes and all objects are instances of the object class.

In the multiple inheritance scenario, any specified attribute is searched first in the current class. If not found, the search continues into parent classes in depth-first, left-right fashion without searching the same class twice.

This order is also called linearization/lɪnɪəraɪˈzeɪʃən/线性化 of MultiDerived class and the set of rules used to find this order is called Method Resolution Order (MRO).

MRO must prevent local precedence ordering and also provide monotonicity. It ensures that a class always appears before its parents. In case of multiple parents, the order is the same as tuples of base classes.

MRO of a class can be viewed as the mro attribute or the mro() method. The former returns a tuple while the latter returns a list.

Mixins

Mixins are an alternative class design pattern that avoids both single-inheritance class fragmentation and multiple-inheritance diamond dependencies.

A mixin is a class that defines and implements a single, well-defined feature. Subclasses that inherit from the mixin inherit this feature—and nothing else.

posted @ 2021-01-12 18:42  fengshili0721  阅读(65)  评论(0编辑  收藏  举报