Object-oriented 18.Python of: Inheritance

One: What is object-oriented inheritance?

Compare the official statement is:

  Inheritance (English: inheritance) is a concept of object-oriented software technology among.

If a category A "inherited from" Another category B, put the A is called "sub-category B"

And B is called the "father of category A" can also be called "B of A is a superclass." Inheritance can make sub-categories

Having various properties and methods of the parent class, without the need to write the same code again. In Reiko

Classes inherit parent category, while certain properties can be redefined, and override certain methods, i.e. coating

Original properties and methods cover the parent category, so to get the parent category different functions. Further, subclass

Do not append new properties and methods are common practice. General static object-oriented programming languages, inheritance

Are static, meaning the behavior of sub-categories have been decided at compile time and can not expand in the implementation period.

Literally means: his father's footsteps, the legal successor of the family property, that is, if you are an only child, and you

Very filial piety, nothing else, you will inherit your parents all the houses, and all of their property will be used by you

(Except prodigal children).

So it inherited with a look at an example:

class Person:
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex

class Cat:
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex

class Dog:
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex

# Inherited usage:
class Aniaml (Object):
DEF __init __ (Self, name, Sex, Age):
self.name = name
self.age = Age
self.sex = Sex


class Person(Aniaml):
pass

class Cat(Aniaml):
pass

class Dog(Aniaml):
pass

Inherited little is obvious:

1, an increase of the class coupling (coupling not appropriate, should be fine).

2, reducing the repeated code.

3, makes the code more standardized, rationalized.

Two inherited classification

On the above example:

Aminal called the parent class, the base class, superclass.
Person Cat Dog: subclass, derived class.
Inheritance: can be divided into single inheritance, multiple inheritance .

We should add species (inherited required) classes in python:

There are two classes in python2x version:
  ⼀ a called Classic before python2.2 ⼀ straight Using the classic root base class class class in classic if nothing to write....
  ⼀ called a new category in. python2.2 appeared after the new class. the new features is the root class is the base class for the object class.
python3x version is only one class:
python3 Use of manipulation are the new class . If the base class who do not inherit the class object inherits by default

III. Single inheritance

3.1 class name, object to perform the parent class method

Aniaml class (Object):
TYPE_NAME = 'animal'

def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex

EAT DEF (Self):
Print (Self)
Print ( 'eat')


class Person(Aniaml):
pass


class Cat(Aniaml):
pass


class Dog(Aniaml):
pass

# Class name:
Print (Person.type_name) # can call the parent class attributes and methods.
Person.eat (111)
Print (Person.type_name)

Object #:
# instantiated objects
p1 = Person ( 'Chun', 'M', 18 is)
Print (P1 .__ dict__ magic)
properties of the parent class object is performed # class method.
Print (p1.type_name)
p1.type_name = '666'
Print (P1)
p1.eat ()

Class name, object calls the parent class method, respectively

3.2 execution order

class Aniaml(object):
type_name = '动物类'
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex

EAT DEF (Self):
Print (Self)
Print ( 'eat')

class Person(Aniaml):

def eat(self):
print('%s 吃饭'%self.name)

class Cat(Aniaml):
pass

class Dog(Aniaml):
pass

p1 = Person ( 'barry', ' M', 18)
to be performed when the object is instantiated # __init__ method, not a class, looking from a parent class, not the parent class, looking from the object class.
p1.eat ()
# method must first perform eat their own class, there is no way to perform their own class in the parent class.

Execution order

3.3 classes and parent classes while performing methods

method one:

If you want to perform the func methods of the parent class, subclass this method and night use, then write in the method of the subclass:

.FUNC parent class (object other parameters)

for example:

class Aniaml(object):
type_name = '动物类'
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex

EAT DEF (Self):
Print ( 'eat')

class the Person (Aniaml):
DEF __init __ (Self, name, Sex, Age, Mind):
'' '
Self = p1
name =' Chun '
Sex =' laddboy '
Age = 18
Mind =' thinking '
' ''
# Aniaml .__ init __ (self, name , sex, age) # The method
self.mind = mind

def eat(self):
super().eat()
print('%s 吃饭'%self.name)
class Cat(Aniaml):
pass

class Dog(Aniaml):
pass

# Method one: .__ Aniaml the init __ (Self, name, Sex, Age)
# p1 = the Person ( 'Chun', 'laddboy', 18, ' thinking')
# Print (p1 .__ dict__)

# If the method does not understand a:
# DEF FUNC (Self):
# Print (Self)
# =. 3 Self
# FUNC (Self)

Method Two:

Using super, super (). Func (parameter)

class Aniaml(object):
type_name = '动物类'
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex

EAT DEF (Self):
Print ( 'eat')

class the Person (Aniaml):
DEF __init __ (Self, name, Sex, Age, Mind):
'' '
Self = p1
name =' Chun '
Sex =' laddboy '
Age = 18
Mind =' thinking '
' ''
# super (Person, self) .__ init __ (name, sex, age) # method two
super () .__ init __ (name , sex, age) # method two
self.mind = mind

def eat(self):
super().eat()
print('%s 吃饭'%self.name)
class Cat(Aniaml):
pass

Dog class (Aniaml):
Pass
# p1 = the Person ( 'Chun', 'laddboy', 18, ' thinking')
# Print (p1 .__ dict__)

Single inheritance exercises:

# 1
class Base:
def __init__(self, num):
self.num = num
def func1(self):
print(self.num)

Foo class (Base):
Pass
obj = Foo (123)
obj.func1 () # 123 when running a Base is to func1

# 2
class Base:
def __init__(self, num):
self.num = num
def func1(self):
print(self.num)
class Foo(Base):
def func1(self):
print("Foo. func1", self.num)
obj = Foo(123)
obj.func1() # Foo. func1 123 运⾏的是Foo中的func1

. 3 #
class Base:
DEF the __init __ (Self, NUM):
self.num NUM =
DEF func1 (Self):
Print (self.num)
class Foo (Base):
DEF func1 (Self):
Print (. "Foo func1", self.num)
obj = Foo (123)
. obj.func1 () # 123 when running a Foo func1 is in Foo func1
#. 4
class Base:
DEF the __init __ (Self, NUM):
self.num NUM =
DEF func1 (Self ):
Print (self.num)
self.func2 ()
DEF func2 (Self):
Print ( "Base.func2")
class Foo (Base):
DEF func2 (Self):
Print ( "Foo.func2")
obj = Foo (123)
obj.func1 () Base # 123 Foo.func2 func1 is in the submenus are func2 class
# again
class Base:
def __init__(self, num):
self.num = num
def func1(self):
print(self.num)
self.func2()
def func2(self):
print(111, self.num)
class Foo(Base):
def func2(self):
print(222, self.num)
lst = [Base(1), Base(2), Foo(3)]
for obj in lst:
obj.func2() # 111 1 | 111 2 | 222 3

# Again
class Base:
DEF the __init __ (Self, NUM):
self.num NUM =
DEF func1 (Self):
Print (self.num)
self.func2 ()
DEF func2 (Self):
Print (111, self.num)
class foo (Base):
DEF func2 (Self):
Print (222, self.num)
LST = [Base (. 1), Base (2), foo (. 3)]
for obj in LST:
obj.func1 () that pen # Come on. good count

IV. Multiple Inheritance

class ShenXian: # fairy
DEF FEI (Self):
Print ( "immortal will ⻜")
class Monkey: # monkey
DEF chitao (Self):
Print ( "Monkey submenus like to eat peaches submenus")
class SunWukong (ShenXian, Monkey): # monkey King is immortal, but also ⼀ monkeys
Pass
SXZ = SunWukong () # monkey King
sxz.chitao () # will eat peaches submenus
sxz.fei () # will ⻜

At this point, the Monkey King is the only monkey ⼀ submenus, but also ⼀ a god. That Monkey inherited these two categories. Monkey natural coloring can Perform these two classes of Remedies.

 Using multiple inheritance is simple. Also well understood, but multiple inheritance, there are so ⼀ a problem. When two ⽗ class appeared in the same name Remedies time.

Then how to do it? Then it comes to how to find such a problem ⽗ ⼀ class Remedies That MRO (method resolution order) problem.

In python ⼀ this is a very complex issue because the Use of different versions of python manipulation are different algorithms to complete the MRO.

We should add species (inherited required) classes in python:

There are two classes in python2x version:
  ⼀ a called Classic before python2.2 ⼀ straight Using the classic root base class class class in classic if nothing to write....
  ⼀ called a new category in. python2.2 appeared after the new class. the new features is the root class is the base class for the object class.
python3x version is only one class:
python3 Use of manipulation are the new class . If the base class who do not inherit the class object inherits by default

Multiple inheritance is 4.1 Classic

Although there is no Classic in python3 in. But the classic class MRO'd better learn ⼀ school. This is ⼀ kind of tree traversal ⼀ most straightforward cases.

In the inheritance hierarchy in python we can put into classes and class hierarchy diagram ⼀ a tree structure to, the code:

class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
class E:
pass
class F(D, E):
pass
class G(F, D):
pass
class H:
pass
class Foo(H, G):
pass

The sample code

Mro can deal with this drawing:

 

 Inheritance diagrams have. How it into ⾏ find it? Remember ⼀ principle. In the classic category are recorded using a depth-first traversal ⽅ case. What is depth-first.

⼀ road is coming to an end and then back again. Keep looking to record the next one.

 

 

Figure each circle is ready to send egg address. Arrows and lines displayed in black and line table. That tells you the order to send the eggs into the right to send in the most under-connector ⾯ R. and must be left. Then how to send it?

 

 

As shown in FIG. Definitely in that order 123456 to send. That this order is called depth-first traversal. ⽽ if it is 142,356? This is called Wide-of-first traversal. Okay.

So much depth-first. So how to find the map on ⾯ it? What is MRO? Very simple. Remember that. Scratch from left to right. ⼀ road went head and back.

Continue ⼀ road went head. MRO algorithm is the classic class. 

4.2.1 mro sequence

MRO is an ordered list L, when the class is created it is calculated.
General formula is:

MRO (Child (Base1, Base2)) = [Child] + Merge (MRO (of Base1), MRO (Base2 are looked up), [of Base1, Base2 are looked up]) 
(where Child inherited from Base1, Base2)

Class MRO: Foo-> H -> G -> F -> E -> D -> B -> A -> C. You got it?

If a base class to inherit: class B (A) 
time sequence B is mro

mro( B ) = mro( B(A) )
= [B] + merge( mro(A) + [A] )
= [B] + go ([A] + [A])
= [B, A] 
if the succession to the plurality of base classes: class B (A1, A2, A3 ...) 
MRO time sequence of B
mro(B) = mro( B(A1, A2, A3 …) )
= [B] + merge( mro(A1), mro(A2), mro(A3) ..., [A1, A2, A3] )
= ...
计算结果为列表,列表中至少有一个元素即类自己,如上述示例[A1,A2,A3]。merge操作是C3算法的核心。

4.2.2. 表头和表尾
表头: 
  列表的第一个元素

表尾: 
  列表中表头以外的元素集合(可以为空)

示例 
  列表:[A, B, C] 
  表头是A,表尾是B和C

4.2.3. 列表之间的+操作
+操作:

[A] + [B] = [A, B]
(以下的计算中默认省略)
---------------------

merge操作示例:

如计算merge( [E,O], [C,E,F,O], [C] )
有三个列表 : ① ② ③
merge不为空,取出第一个列表列表①的表头E,进行判断
各个列表的表尾分别是[O], [E,F,O],E在这些表尾的集合中,因而跳过当前当前列表
取出列表②的表头C,进行判断
C不在各个列表的集合中,因而将C拿出到merge外,并从所有表头删除
merge( [E,O], [C,E,F,O], [C]) = [C] + merge( [E,O], [E,F,O] )
进行下一次新的merge操作 ......
---------------------

 

计算mro(A)方式:

mro(A) = mro( A(B,C) )

原式= [A] + merge( mro(B),mro(C),[B,C] )

mro(B) = mro( B(D,E) )
= [B] + merge( mro(D), mro(E), [D,E] ) # 多继承
= [B] + merge( [D,O] , [E,O] , [D,E] ) # 单继承mro(D(O))=[D,O]
= [B,D] + merge( [O] , [E,O] , [E] ) # 拿出并删除D
= [B,D,E] + merge([O] , [O])
= [B,D,E,O]

mro(C) = mro( C(E,F) )
= [C] + merge( mro(E), mro(F), [E,F] )
= [C] + merge( [E,O] , [F,O] , [E,F] )
= [C,E] + merge( [O] , [F,O] , [F] ) # 跳过O,拿出并删除
= [C,E,F] + merge([O] , [O])
= [C,E,F,O]

原式= [A] + merge( [B,D,E,O], [C,E,F,O], [B,C])
= [A,B] + merge( [D,E,O], [C,E,F,O], [C])
= [A,B,D] + merge( [E,O], [C,E,F,O], [C]) # 跳过E
= [A,B,D,C] + merge([E,O], [E,F,O])
= [A,B,D,C,E] + merge([O], [F,O]) # 跳过O
= [A,B,D,C,E,F] + merge([O], [O])
= [A,B,D,C,E,F,O]
---------------------

结果OK. 那既然python提供了. 为什么我们还要如此⿇烦的计算MRO呢? 因为笔
试.......你在笔试的时候, 是没有电脑的. 所以这个算法要知道. 并且简单的计算要会. 真是项⽬
开发的时候很少有⼈这么去写代码. 
这个说完了. 那C3到底怎么看更容易呢? 其实很简单. C3是把我们多个类产⽣的共同继
承留到最后去找. 所以. 我们也可以从图上来看到相关的规律. 这个要⼤家⾃⼰多写多画图就
能感觉到了. 但是如果没有所谓的共同继承关系. 那⼏乎就当成是深度遍历就可以了

Guess you like

Origin www.cnblogs.com/xuweng/p/12181526.html