Class Inheritance (상속)
- 기존에 정의해둔 클래스의 기능을 그대로 물려받을 수 있다.
- => 해당 클래스의 메소드나 속성을 따로 정의하지않고 그대로 사용가능
- 기존 클래스에 기능 일부를 추가하거나, 변경하여 새로운 클래스를 정의한다.
- 코드를 재사용할 수 있게된다.
- 상속 받고자 하는 대상인 기존 클래스는 (Parent, Super, Base class 라고 부른다.)
- 상속 받는 새로운 클래스는(Child, Sub, Derived class 라고 부른다.)
- 의미적으로 is-a관계를 갖는다
- 학생(자식) is a person(부모)
- 직원(자식) is a person(부모)
# 아래 student, employee 클래스의 공통기능을
# 부모 클래스 person으로 뺀다
# 학생 is a person
class Person: #부모 class
def __init__(self, name, age):
self.name = name #속성
self.age = age
def eat(self, food): #method
print('{}은 {}를 먹습니다.'.format(self.name, food))
def sleep(self, minute):
print('{}은 {}분동안 잡니다.'.format(self.name, minute))
def work(self, minute):
print('{}은 {}분동안 일합니다.'.format(self.name, minute))
#person클래스를 부모로, 상속받을꺼야
class Student(Person): #자식 클래스. 속성만 정의한다(method는 부모클래스에서 상속)
def __init__(self, name, age):
self.name = name
self.age = age
class Employee(Person):
def __init__(self, name, age):
self.name = name
self.age = age
bob = Employee('Bob', 25) #employee 클래스로 bob이라는 객체를 생성
# 부모클래스의 method eat, sleep, work
bob.eat('BBQ') #bob객체에서 eat 메소드를 불러와
bob.sleep(30)
bob.work(60)
print('---')
#자식 클래서 student를 ai객체 생성
ai=Student('dd', 22)
ai.eat('wer')
method override
- 부모 클래스의 method를 자식클래스에서 재정의(override)
- 하위 클래스(자식 클래스)의 인스턴스로 호출시, 재정의된 메소드가 호출됨
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{}은 {}를 먹습니다.'.format(self.name, food))
def sleep(self, minute):
print('{}은 {}분동안 잡니다.'.format(self.name, minute))
def work(self, minute):
print('{}은 {}분동안 일합니다.'.format(self.name, minute))
# work는 person(부모)클래스에도 있지만, 학생(자식) 클래스에도 있다면?
# 학생클래스가 work 메소드를 재정의한것. 자식클래스의 메소드로 호출된다
class Student(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
print('{}은 {}분동안 공부합니다(학생_자식).'.format(self.name, minute))
class Employee(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
print('{}은 {}분동안 업무를 합니다(직원_자식).'.format(self.name, minute))
#요기도 수정
bob = Person('Bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60)
print('-----')
bob = Student('Bob', 25)
bob.eat('BBQ')
bob.sleep(30)
bob.work(60) #요기가 달라져서 수행됨
#자식의 기능으로 호출됨. 메소드가 오버라이드되
super
- 하위클래스(자식 클래스)에서 부모클래스의 method를 호출할 때 사용
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{}은 {}를 먹습니다.'.format(self.name, food))
def sleep(self, minute):
print('{}은 {}분동안 잡니다.'.format(self.name, minute))
def work(self, minute):
print('{}은 {}분동안 준비를 합니다.'.format(self.name, minute))
#부모클래스를 이용하고싶어
class Student(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
super().work(minute) #부모클래스의 함수를 다시부를 수 있다
print('{}은 {}분동안 공부합니다.(super)'.format(self.name, minute))
class Employee(Person):
def __init__(self, name, age):
self.name = name
self.age = age
def work(self, minute):
super().work(minute)
print('{}은 {}분동안 업무를 합니다.(super)'.format(self.name, minute))
bob = Employee('Bob', 25)
# 직원 클래스에서 eat, sleep은 부모에만 존재. 자식에서 재정의하지도, super로 둘다나오지도 않았어.
bob.eat('BBQ')
bob.sleep(30)
print('--')
#오버라이드가 아니고, 둘다 나오려면 super하면 부모 자식 둘다나와
bob.work(60)
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def eat(self, food):
print('{}은 {}를 먹습니다.'.format(self.name, food))
def sleep(self, minute):
print('{}은 {}분동안 잡니다.'.format(self.name, minute))
def work(self, minute):
print('{}은 {}분동안 준비를 합니다.'.format(self.name, minute))
class Bigyo(Person):
def __init__(self, name, age):
self.name = name
self.age = age #여기까지만하면, 부모클래스를 상속받게됨
#여기부터 자식클래스의 method인데, 동일 def라면, 자식클래스가 호출
def work(self, minute):
print('{}은 {}분동안 일합니다(자식이 override).'.format(self.name, minute))
#super를 쓰면, 부모, 자식 둘다 나옴
def eat(self, food):
super().eat(food)
print('{}은 {}를 먹습니다(super).'.format(self.name, food))
bigyo1=Bigyo('tom', 20)
# work 메소드는 자식클래스에서 재정의
bigyo1.work(20)
print('----')
#eat 메소드는 super. 부모, 자식 메소드 둘다나옴
bigyo1.eat('gim')