一、类的继承
1.类的3个特性,封装、继承、多态,前面定义类的属性和方法都是属于封装。
2.继承关系:继承是相对两个类而言的父子关系,子类继承了父类的
所有公有属性和方法,
继承最大的好处就是实现了代码重用.
3.继承可以重用已经存在的数据和行为,减少代码的重复编写,Python在类名后使用一对括号来表示继承关系,括号中的类即为父类。
class People(object):
color = 'yellow'
__age = 30
def think(self):
self.color = 'black'
print(self.color)
class Cinese(People): #定义一个子类,括号里写父类的名字
pass
cn = Chinese() #实例化子类
print cn.color #调用父类的属性
可以看到完美的继承了父类。这是一个简单的继承。
如果父类里有构造函数呢。
class People(object):
color = 'yellow'
__age = 30
def __init__(self):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print(self.color)
class Cinese(People): #定义一个子类,括号里写父类的名字
pass
cn = Chinese() #实例化子类
print cn.dwell #调用父类的属性
.
继承了父类里的构造函数。不过当我们的父类的构造函数有2个以上的参数,那么就调用不出来了。
怎么才能调用出来呢?
class People(object):
color = 'yellow'
__age = 30
def __init__(self, c): #当参数大于等于2的时候,子类就必须显示的调用
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print(self.color)
class Cinese(People):
def __init__(self): #定义子类的构造函数
People.__init__(self,'red') #调用父类的构造函数。
cn = Chinese()
print cn.dwell
父类名.__init__(self.'red') 父类定义了几个参数, 子类调用里就要写几个参数。
4.定义类其实有2中方法,一种是我们现在用的new stryle。还有一种是传统的定义。class People: 连括号都没有。
class People:
color = 'yellow'
__age = 30
def __init__(self, c):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print(self.color)
class Cinese(People):
def __init__(self):
People.__init__(self,'red')
cn = Chinese()
print cn.dwell
传统定义也可以。
5.super()函数,调用父类我们还可以使用super函数。但是super函数只支持newstyle的写法。
class People(object):
color = 'yellow'
__age = 30
def __init__(self, c):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print(self.color)
class Cinese(People):
def __init__(self):
super(Chinese,self).__init__('red') #使用super函数来调用
cn = Chinese()
print cn.dwell
super函数的语法是super(子类名,self).构造函数名(参数)
如果父类定义了构造函数,子类也要写构造函数来调用父类的构造函数
还可以去修改父类的方法,修改后调用的是自己的方法,而不是父类的方法了。
class People(object):
color = 'yellow'
__age = 30
def __init__(self, c):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print(self.color)
class Cinese(People):
def __init__(self):
super(Chinese,self).__init__('red')
def think(self): #名字和父类的方法名字一样,就可以修改父类的方法了
print("i am a thinker")
cn = Chinese()
cn.think() #调用的是自己的think方法,而不是父类的。
二、多重继承
1.Python支持多重继承,即一个类可以继承多个父类。
语法是:class class_name(parent_c1,parent_c2)
注意:当父类中出现多个自定义的__init__方法时,多重继承只执行第一个类的__init__方法,其他不执行。
class People(object):
color = 'yellow'
__age = 30
def __init__(self):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print("my home is %s"% self.dwell)
class Martian(object):
color = 'red'
def __init__(self):
self.dwell = 'Martian'
class Chinese(People,Martian):
pass
cn = Chinese()
cn.think()
可以看到继承第一个父类People的构造函数里的值。接着调换下位子看看
class People(object):
color = 'yellow'
__age = 30
def __init__(self):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print("my home is %s" % self.dwell)
class Martian(object):
color = 'red'
def __init__(self):
self.dwell = 'Martian'
class Chinese(Martian,People):
pass
cn = Chinese()
cn.think()
进行了多重继承,只能继承第一个类的构造函数,那个类在前面就继承这个类的构造函数。
如果想要调用第二个父类的构造函数,可以是用显式的方式去调用
class People(object):
color = 'yellow'
__age = 30
def __init__(self):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print("my home is %s" % self.dwell)
class Martian(object):
color = 'red'
def __init__(self):
self.dwell = 'Martian'
class Chinese(Martian,People):
def __init__(self):
People.__init__(self) #调用第二个父类的构造函数,这就没有调用第一个父类的构造函数了。
cn = Chinese()
cn.think()
这样就继承了第二个父类的构造函数。
2.多重继承和位子很有关系,谁写在前面调用谁的方法,当然如果显式的定义了就会调用显示的父类构造函数。
class People(object):
color = 'yellow'
__age = 30
def __init__(self):
self.dwell = 'Earth'
def think(self):
self.color = 'black'
print("my home is %s" % self.dwell)
class Martian(object):
color = 'red'
def __init__(self):
self.dwell = 'Martian'
class Chinese(Martian,People):
def __init__(self):
People.__init__(self)
def talk(self):
print("i am a talker")
cn = Chinese()
cn.think()
cn.talk()
这只对构造函数起效果,在父类里定义的公有方法,都可以被继承。