19. Object-Oriented (inherited)

Object-oriented three properties: encapsulation, inheritance, polymorphism

The concept of inheritance:

  When you define a class, you can extract the desired content from among existing class

  Inherited class is called the parent class, the base class, super class, newly defined class is called a subclass, derived class

  Note: If the property with the same name as the base class properties in a derived class, the derived class attributes will overwrite properties of the base class. Including initialization function.

    Derived classes inherit the need to modify the initialization function and the initialization process, the use of 'class name + __ __ (arg) init' to private inheritance characteristics and may be used super () function.

Inherited advantages:

  1. save code

  2. Regulation code

Inherited action

  One of the major benefits of object-oriented programming brings is to reuse code reuse to achieve this, one way is through the inheritance mechanism. Inheritance can fully understand the relationship between types and subtypes into classes.

 

Acquaintance Inheritance:

  Only the present method of performing class

  Method of performing only the parent class

  The method performs both the parent class and method of implementation of the class

    The name of the parent class. Class name (parameters)

    super (). Method name (parameter (self automatic transmission parameter value))

class

  The new category: those who inherited object classes are the new class

    python3x all classes for the new class, because python3x the class object inherits by default

  Classic: object classes are not inherited Classic

    python2x (both new-style class, there are Classic) all classes do not inherit the default object class, default to the classic class can be inherited as a new class with the object

Inherited Category:

  1. Single inheritance

    The new class and the Classic as a query sequence

  2. Multiple Inheritance

    The new category: follow the breadth-first

      A road go in the penultimate stage, to determine if other road can be a road go low, go the other way is returned, if not, to an end

    Classic: follow depth-first

      A road go low

Breadth-first diagrammed below:

code show as below:

# class A:
#     pass
#     # def func(self):
#     #     print('IN A')
# 
# class B(A):
#     pass
#     # def func(self):
#     #     print('IN B')
# 
# class C(A):
#     # pass
#     def func(self):
#         print('IN C')
# 
# class D(B):
#     pass
#     # def func(self):
#     #     print('IN D')
# 
# class E(C):
#     # pass
#     def func(self):
#         print('IN E')
# 
# class F(D,E):
#     pass
#     # def func(self):
#     #     print('IN F')
# 
# f1 = F()
# f1.func()

 

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11620859.html