'''
오버라이딩(overriding) 메서드
개념:
부모에 있는 메서드를 그냥 사용하지 않고
수정(재정의)해서 사용한 메서드 의미.
목적: 재사용
class Pet:
def a(self):
print("hello")
class Cat(Pet):
* 아쉬운 점은 강제성이 없다. 자식으로 이전처럼 자기 마음대로 메서드를 만들어서 사용해도 무관.
===> 해결: ( python: 추상 클래스 )
(자바: 추상클래스 ,인터페이스 )
'''
print(type('str'), type(10))
class Pet:
def __init__(self, username, age): # 공통적인 속성을 추출해서 부모인 Pet에서 선언
# 공통적인 속성 추출
self.username = username
self.age = age
# 공통적인 기능 추출
def eat(self):
print("Pet eat")
def pet_print(self):
print(self.username, self.age)
class Cat(Pet):
def __init__(self, username, age, gender): # 이름, 나이, 성별 3가지 속성을 가진 Cat
# self.username = username
# self.age = age
super().__init__(username,age) # 부모생성자를 호출하면서 부모를 먼저 생성함.
# 동시에 부모에서 선언된 username과 age 값을 초기화하기 위해서
# 로컬값을 전달함
self.gender = gender
# 오버라이딩 메서드
def pet_print(self):
print(self.username, self.age, self.gender)
# 고양이 먹기
# 오버라이딩 메서드 ( 부모의 eat 메서드를 재정의 )
def eat(self):
print("Cat eat")
class Dog(Pet):
def __init__(self, username, age, color):
super().__init__(username, age)
self.color = color
# 오버라이딩 메서드
def pet_print(self):
print(self.username, self.age, self.color)
# 오버라이딩 메서드 ==> 재사용이 되기 때문에 일관된 메서드(기능)를 사용 가능하다. (******)
def eat(self):
print("Dog eat")
c1 = Cat("야옹이", 2, "암컷")
c1.pet_print()
c1.eat() #
d1 = Dog("망치",1, "흰색")
d1.pet_print()
d1.eat()
# 계층구조
print(Cat.mro())