Python類(マルチ継承+ダイヤモンド継承)


クラス-マルチ継承+ダイヤモンド継承
class Person:
    def greeting(self):
        print('안녕하세요.')
 
class University:
    def manage_credit(self):
        print('학점 관리')
 
class Undergraduate(Person, University):
    def study(self):
        print('공부하기')
 
james = Undergraduate()
james.greeting()         # 안녕하세요.: 기반 클래스 Person의 메서드 호출
james.manage_credit()    # 학점 관리: 기반 클래스 University의 메서드 호출
james.study()            # 공부하기: 파생 클래스 Undergraduate에 추가한 study 메서드

""" 다이아몬드 상속
    파이썬은 이럴 경우, 왼쪽에 있는 클래스부터 탐색하게됨.
    클라스 탐색 순서는 클래스 D 에 mro 메서드를 호출해보면 상속 관계를 알 수 있음.
    D.mro() """

class A:
    def greeting(self):
        print('안녕하세요. A입니다.')
 
class B(A):
    def greeting(self):
        print('안녕하세요. B입니다.')
 
class C(A):
    def greeting(self):
        print('안녕하세요. C입니다.')
 
class D(B, C):
    pass
 
x = D()
x.greeting()    # 안녕하세요. B입니다.