Inheritance, inheritance 10.10 class, derive new classes, Classic

Class inheritance

What is inherited

Inheritance is a new class of methods, the new class is called the child class or a derived class, subclass inherits the class will call the parent class, also called a base class or superclass.

Inherited characteristics

Subclass can inherit the properties of the parent class (characteristics and skills), and can derive their own attributes (characteristics and skills).

note

python in a plurality of subclasses inherit the parent class, other languages ​​only a subclass inherits a parent class.

Why should inherit

Inheritance purpose is to reduce the repetition code.

How succession

You must first determine who is a subclass, who is the parent class.
When you define a class, subclass + (), write the parent class (), the implementation inheritance.

class 父:
    pass
class 子(父):
    pass

View the inherited parent class

__bases__Is a property of the class, is used to find the father of the current class.
print(Class1.__bases__)

How to find inheritance

First you have to abstraction, inheritance

What is the abstract

Extracting like parts, called Abstract

Inheritance

Object is a combination of features and skills
-based target is a combination of a series of identical characteristics and skills
inheritance class is a combination of a series of identical characteristics and skills

In the context of inheritance, object attributes search order

Object lookup property will first search from the namespace object
if the object does not, then go to class namespace lookup
if the current is a subclass, and no, the name of the parent class space will go find, not on the error
object lookup property, if there subclass, regardless of the parent class has not, subject to the subclass

Derivation

Derived attribute refers to a subclass inherits the parent class, and derive new attributes.
Subclassing a new property, if the parent class attributes are the same places subclass shall prevail.
Inheritance is who is with whom the relationship, referring to the the relationship between class and class is a subclass of parent class affiliation.

Subclass inherits the superclass __init__ meaningless
a Dian directly by 父类.__init__the __init__as ordinary functions use, the incoming object and inheritance function attributes
two Dian super is a special class, subclass to call super () will get a special object, by .pointing to the parent class namespace
two ways do not mix with

The new class of Classic

The new class

Belonging to the breadth-first
inherited object classes are called new class
python3 default inherited object.

Classic

Belong to a depth-first
does not inherit the class object of the class are classic.

vale ()

Belongs to the object, is the type of function, the order of succession to view the current class, in the case of multiple inheritance.

Guess you like

Origin www.cnblogs.com/793564949liu/p/11649945.html
Recommended