day21 inheritance

Yesterday Review

1, object-oriented

Is a programming idea is the core target

The program seen as a bunch of objects set of functions the program is to control the scheduling objects interact complete

2. Why use an object-oriented

Advantage of high scalability and flexibility, reusability,

The disadvantage is more complex program structure can not predict the results

3, classes and objects

Class is a series with the same characteristics and behavior aggregate object is an abstract concept

The object is to have a collection of things are certain characteristics and behaviors specific existence

Class contains a plurality of objects

Objects belonging to a type

4, the definition of

5, the proper use of the property

Public class objects into portions

Each object in the object's own unique namespace

Deletions attribute change search

6, initialization function

For attachment to the object and the initial value of some other initialization logic

The return value can not be returned none

7, binding method

Binding method of the object and the function object

When an object is automatically invoked incoming object itself as the first argument

When a class is an ordinary function calls that need their own traditional values

The class and binding function classes

With a class or object calls will pass the class itself

​ @classme

8, non-binding approach

Scene

When the logical function of access to the object bound to a subject in need then only the class data is bound to the class

Find the order to go first object properties can not find its own namespace can not find his parent looking for the class namespace classes.

Content Today

One of the three characteristics of oop

Polymorphism package inheritance

inherit

What is inherited

Inheritance is a relationship through inheritance, inherited a party can be used as one of the party has inherited some things, inherited a party called the parent, the child is called inheritance.

When the inevitable inheritance python3 create another class If not specify a parent class, the default inherited object; object is with all classes that are directly or indirectly inherited object.

Why use inheritance

1, reducing code duplication improve reusability

2, polymorphic provide the necessary support

In the program, it is the inheritance relationships between classes and class description

grammar

class 类名称(父类的名称):
    类的内容





#在python中  一个子类可以继承多个父类

Abstraction and inheritance (first abstract and then inherited):

What is the abstract

The same portion of the plurality of subclasses extraction, formation of a new class of this process is also called abstraction.

Find the order

First find the object's own class resides ---- ---- ---- find parent parent parent class ---- Object

Abstract usage scenarios

Inheritance is based on the abstract result, the programming language to implement it, certainly is the first experience of this abstract process, in order to express the abstract structure by way of inheritance.

Just an abstract process analysis and design, a movement or a skill, can get through abstract class

1564060859005

Derivation

When a subclass occurred with different content parent class, subclass it is called a derived class

Usually sub-class will write some new code and can not be exactly like the parent that is usually derived class

So derived class refers to a subclass

cover

When the subclasses appears entirely consistent with the parent class and subclass name attribute or method

Exercise restriction element can achieve a type of container (neuron progenitor dictionary listing a collection of strings)

Subclass the parent class content access

grammar

方式一:
super(当前类名称,self).你要调的父类的属性或方法
方式二:
super().你要调的父亲的属性或方法
方式三:
类名称.你要调的父类的属性或方法(self)
方式三与继承无关
    

Emphasize

When you inherit an existing class and you cover the parent class init method, you must call the parent class initialization method of the first line of the initialization method, passing in the required parameters parent class

combination

What kind of relationship will describe what is the relationship between the two objects

An object as a property of another object that is what what

The purpose combination

That is, to reuse existing code

When to use inheritance relationship analysis of two classes in the end is not what is the relationship between what

When used in combination if there is no significant relationship between the two classes are not completely similar

Further lower compared to the combination of inherited coupling

Diamond inheritance

First clear python support multiple inheritance

The new supplement category and Classic

python3 in any class are inherited directly or indirectly Object

The new class of any explicit or implicit Object class inherits from class to the new call in all new class python3

Classic neither a subclass of Object appears only in the python2

When there is a diamond inheritance, the new class, the first depth, when faced with a common parent class on breadth

The new class is a depth-first

Guess you like

Origin www.cnblogs.com/panyi2019/p/11265602.html