继承:多继承

 # ### 多继承

# (1) 基本语法
class Father():
	f_property = "风流倜傥,英俊潇洒"
	def f_hobby(self):
		print("吃喝嫖赌抽,坑蒙拐骗偷,喜欢烫头")
		
class Mother():
	m_property = "沉鱼落雁,闭月羞花"
	def m_hobby(self):
		print("打麻将,跳广场舞,喜欢买名牌包包")
		
# 多继承:在括号里面用逗号,把多个父类隔开
class Daughter(Father,Mother):
	pass

obj = Daughter()
print(obj.f_property)
obj.m_hobby()

# (2) 调用父类
class Father():
	f_property = "风流倜傥,英俊潇洒"
	def f_hobby():
		print("吃喝嫖赌抽,坑蒙拐骗偷,喜欢烫头")
		
class Mother():
	m_property = "沉鱼落雁,闭月羞花"
	def m_hobby(self):
		print("打麻将,跳广场舞,喜欢买名牌包包")

"""
(1)super本身是一个类 super()是一个对象 用于调用父类的绑定方法
(2)super() 只应用在绑定方法中,默认自动传递self对象 (前提:super所在作用域存在self)
(3)super用途: 解决复杂的多继承调用顺序
"""
class Son(Father,Mother):
	# f_property = "子类的属性f_property"
	# (1)通过类的方式调用父类的相关成员
	def skill1(self):
		# 调用Father中的f_hobby
		Father.f_hobby()
		# 调用Mother中的m_property
		print(Mother.m_property)
		
	# (2)通过对象的方式调用父类的相关成员
	def skill2(self):
		# 调用Father中的f_property
		print(self.f_property)
		# 调用Mother中的m_hobby
		self.m_hobby()
		
	# (3)使用super调用父类的相关成员
	def skill3(self):
		# 调用Father中的f_property
		# super() 一定调用的是父类的,区别于self.
		# print(super().f_property)
		# super().f_hobby() error  super()调用的是绑定方法,默认传递self参数
		# 调用Mother中的m_hobby
		super().m_hobby()
		
print("<====>")
obj = Son()
# obj.skill1()
# obj.skill2()
obj.skill3()

'''
self 在子类存在这个成员的时候,调用自己的
子类不存在这个成员的时候,调用父类的
而super,是一定只调用父类的公有成员属性,和公有绑定方法.
'''

  

猜你喜欢

转载自www.cnblogs.com/huangjiangyong/p/10920591.html