table of Contents
- Three characteristics of object-oriented
- inherit
Three characteristics of object-oriented
inherit
Package
Polymorphism
inherit
First, what is inherited
Inheritance is a way to create a new class, the new class can inherit one or more parent classes (python support multiple inheritance), the parent class can be called a base class or super class, the new class is called the derived class or subclass . Properties and methods of the parent class, subclass also.
Format is the subclass name in parentheses, brackets is the name of the parent class:
class A:
pass
class B(A): # B就继承了A
pass
The difference here mention the new class and the classic class
The new class: as long as the inherited object class, the new class is, in python3, the default is a new-style class
In python2, you need to specify the display is the new class to inherit objec
Classic: In python2, there is no inheritance object class is the classic class
python3 no Classic
Second, class inheritance is divided into single and multiple inheritance
class A(object):
pass
class B(A):
pass
# B继承了A这个类,单继承
class C(A,B):
pass
# C继承了A和B,多继承
Of other built-in attributes:__名字__
print(B.__dict__) # 类的属性和方法
print(B.__name__) # 类名
print(B.__bases__) # 类的父类
== inheritance in order to find the property: ==
First look for in an object --- "subcategory find ---" to find the parent class (multiple inheritance parent class, then from left to right, starting with the first parent to find) --- "to find the parent class less than a given
Third, the use of inheritance reduce code redundancy
# 这里首先利用面向对象写一个老师和学生的类,他们的基类都是人
# 这里不用继承
class Person:
school = 'oldboy'
class Teacher(Person):
def __init__(self,name,age,level):
self.name=name
self.age=age
self.level=level
class Student(Person):
def __init__(self,name,age,course):
self.name=name
self.age=age
self.course=course
# 以上代码老师和学生的类都有共同属性:name和age,那就可以把这两个属性放到人的类中,然后老师和学生的类来继承人的类
class Person(object):
school = 'oldboy'
def __init__(self, name, age):
self.name = name
self.age = age
class Teacher(Person):
pass
class Student(Person):
pass
# 这样就减少了代码的冗余
Note: sub-class instantiation automatically invokes __init__
method, if not in the subclass, will go to find the parent, the parent class has several parameters must pass several parameters
Fourth, multiple layers and multiple inheritance
# 多层继承:一层一层的继承下去
class A:
a="AAAA"
class B(A):
a="BBB"
class C(B):
a="CCC"
pass
class D(C):
a = "DDD"
pass
# 多继承
class A:
a="AAAA"
class B:
a="BBB"
class C:
a="CCC"
pass
class D(A,B,C): # 类 D 继承了上面多个类
a = "DDD"
pass
# 多继承的多层
class A:
a="AAAA"
class B(A):
a="BBB"
class C(B):
a="CCC"
pass
class D(A,B,C):
a = "DDD"
pass
V. inherited property search order
5.1 diamond problem
Diamond inheritance problem (display of a class are inherited, not the object class): Find new class order and Classic are not the same.
The new class (py3 in all new style classes): breadth first --- Starting from the left, has been looking up to find the diamond in the apex end (not including the diamond-shaped vertex), continue to the next to inherit the parent class looking up, found diamond apex end (not including the diamond-shaped vertex), and finally found a diamond vertices.
Classic (only in py2 only): --- depth-first from the left, has been looking up to find the diamond in the apex end (including diamond vertex) continue to inherit under a parent looking up to find the diamond vertices end (does not include fixed-point diamond).
Diamond problem does not occur: Normal Find
#继承的菱形问题:新式类和经典类的查找顺序是不一样的
#新式类的查找属性:广度优先
#经典类:深度优先
class G(object):
a = "GGG"
pass
class F(G):
# a = "FFF"
pass
class E(G):
# a = "EEE"
pass
class D(G):
# a = "DDD"
pass
class C(F):
# a="CCC"
pass
class B(E):
# a="BBB"
pass
class A(B,C,D):
# a="AAAA"
pass
a=A()
print(a.a)
The above code will appear diamond issue of new classes, breadth-first search order is:
Classic problem of diamond, depth-first search order is:
5.2 According to mro
lookup list
MRO == == : list, the order of succession to find the list (only in the new class)
print(A.mro())
print(A.__mro__)
# 两种方式都可以
Sixth, the parent class method inherited reuse have two ways
Unrelated to the use of naming names, with inheritance: a 6.1 way
# 父类中object写与不写,在py3中没有区别,
# 有的人在py3中这么写,为了向下兼容
# 调用父类方法的第一种方式 :指名道姓的方式,跟继承关系无关
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def init_1(self,name,age):
self.name=name
self.age=age
class Student:
school = 'yyyy'
def __init__(self,name,age,course):
Person.__init__(self,name,age) #指名道姓的使用Person的__init__方法,Preson中有三个参数就必须传三个
init_1(self,name,age)
self.course=course
stu=Student('nick',19,'python')
print(stu.name)
6.2 Second way: through the super keyword, related with inheritance
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
class Student(Person):
school = 'yyyy'
def __init__(self,name,age,course):
#super()相当于得到了一个特殊对象,第一个参数不需要传,调用绑定方法,会把自己传过去
########## self不需要传,不需要传
super().__init__(name,age)
#看到别人这么写:super(类名,对象) 在py3中为了兼容py2
#在py3中这么写和省略写法完全一样
#在py2中必须super(Student,self)写
# super(Student,self).__init__(name,age)
self.course=course
stu=Student('nick',19,'python')
print(stu.name)
print(stu.age)
print(stu.course)
6.3 summary
There inheritance of time, usually super keyword method;
The method used by name in the following cases:
- No inheritance
- If you inherit multiple parent classes, super accordance mro list to find, now want to name names using one of the methods of a parent class, we need to name names of Use