• 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.

  • 相关阅读:
    selenium——上传文件
    selenium——下拉框
    selenium——鼠标操作ActionChains:点击、滑动、拖动
    selenium定位元素—逻辑运算、xpath函数、轴定位
    spring设置webAppRootKey
    树莓3 集群
    redis集群搭建
    MQ基础
    MQ集群测试环境搭建(多节点负载均衡,共享一个kahaDB文件(nas方式))
    weblogic对jms实现的QueueConnection实现与TopicConnection实现问题
  • 原文地址:https://www.cnblogs.com/fengshili666/p/14268673.html
Copyright © 2020-2023  润新知