method override(재정의)
class Parent:
def printData(self):
pass
class Child1(Parent):
def printData(self): # method override
print('Child1에서 재정의')
class Child2(Parent):
def printData(self): # method override
print('Child2에서 재정의')
print('오버라이드는 부모의 메소드를 자식이 재정의')
def abc(self):
print('Child2 고유 메소드')
c1 = Child1()
c1.printData()
print()
c2 = Child2()
c2.printData()
print('다형성 -----')
par = Parent()
par = c1
par.printData()
print()
par = c2
par.printData()
par.abc()
print()
plist =[c1,c2]
for i in plist:
i.printData()
다중 상속 : 순서가 중요
class Tiger:
data = '호랑이 세상'
def cry(self):
print('호랑이는 어흥')
def eat(self):
print('맹수는 고기를 좋아함.')
class Lion:
def cry(self):
print('사자는 으르렁')
def hobby(self):
print('백수의 왕은 낮잠을 즐김')
class Liger1(Tiger, Lion): # 다중 상속
pass
a1 = Liger1()
a1.cry()
a1.eat()
a1.hobby()
print(a1.data)
print('----------')
class Liger2(Lion, Tiger):
data = '라이거 만세'
def hobby(self):
print('라이거는 자바를 싫어해')
def showData(self):
print(self.data, ' ', super().data)
self.hobby()
super().hobby()
a2 = Liger2()
a2.cry()
a2.eat()
a2.hobby() # 우선순위가 있다.
a2.showData()