58 繼承2

2020-08-08 20:26:22
# 2 在子類派生的新方法中重用父類別功能的方式二
# 方式一:呼叫super(自己的類名,self)會返回一個特殊的物件,super(自己的類名,self).屬性,會參照屬性查詢發起的那個類的mro列表去它父類別中查詢屬性
# 特點:嚴格依賴於繼承關係
class OldboyPeople:
	school = "oldboy"

	#             空物件,"艾利克斯",73,'male'
	def __init__(self, name, age, gender):
		self.name = name
		self.age = age
		self.gender = gender

	def f1(self):
		print('1111111')


class Student(OldboyPeople):
	def __init__(self, name, age, gender, stu_id, course):
		# OldboyPeople.__init__(self,name,age,gender)  # OldboyPeople.__init__(空物件,"艾利克斯",73,'male')
		super(Student, self).__init__(name, age, gender)
		self.stu_id = stu_id
		self.course = course

	def choose(self):
		print('%s 正在選課' % self.name)

	def f1(self):
		# OldboyPeople.f1(self)
		# super().f1()
		print("22222")


# print(Student.mro())
stu1 = Student("艾利克斯", 73, 'male', 1001, "python全棧開放")
# print(stu1.__dict__)
stu1.f1()


# 3:
class A:
	def test(self):
		super().test()


class B:
	def test(self):
		print('from B')


class C(A, B):
	pass


c = C()
print(C.mro())
c.test()

obj = A()
obj.test()


# 4:
class A:
	def test(self):
		print('A---->test')
		super().aaa()


class B:
	def test(self):
		print('B---->test')

	def aaa(self):
		print('B---->aaa')


class C(A, B):
	def aaa(self):
		print('C----->aaa')


c = C()
print(C.mro())
c.test()
"""
A---->test
B---->aaa
"""