multi.py 1.2 KB
Newer Older
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
"""

多重继承
	- 通过多重继承可以给一个类的对象具备多方面的能力
	- 这样在设计类的时候可以避免设计太多层次的复杂的继承关系

Version: 0.1
Author: 骆昊
Date: 2018-03-12

"""


class Father(object):

	def __init__(self, name):
		self._name = name

	def gamble(self):
		print('%s在打麻将.' % self._name)

	def eat(self):
		print('%s在大吃大喝.' % self._name)


class Monk(object):

	def __init__(self, name):
		self._name = name

	def eat(self):
		print('%s在吃斋.' % self._name)

	def chant(self):
		print('%s在念经.' % self._name)


class Musician(object):

	def __init__(self, name):
		self._name = name

	def eat(self):
		print('%s在细嚼慢咽.' % self._name)

	def play_piano(self):
		print('%s在弹钢琴.' % self._name)


# 试一试下面的代码看看有什么区别
# class Son(Monk, Father, Musician):
# class Son(Musician, Father, Monk):


class Son(Father, Monk, Musician):

	def __init__(self, name):
		Father.__init__(self, name)
		Monk.__init__(self, name)
		Musician.__init__(self, name)


son = Son('王大锤')
son.gamble()
# 调用继承自Father的eat方法
son.eat()
son.chant()
son.play_piano()