한번씩 기술 블로그를 볼때마다 IS-A , HAS-A 어쩌고 저쩌고 하던데 컴퓨터 사이언스 부트캠프 with 파이썬 책에 잘 나와있어서 정리해본다

 

IS-A: 상속

 - IS-A는 ~은 ~의 종류다 라는 뜻인데 A laptop IS-A computer 즉 랩탑(노트북)은 컴퓨터의 한 종류다! 이 예시가 제일 적절하다. 그러니깐 컴퓨터는 부모 클래스고 노트북은 자식 클래스이다.

 

컴퓨터와 노트북은 IS-A 상속 관계

class Computer:

    def __init__(self, cpu, ram):
        self.cpu = cpu
        self.ram = ram

    def browse(self):
        print('브라우저로 웹서칭')

    def work(self):
        print('일하기')


class Laptop(Computer):

    def __init__(self, cpu, ram, battery):
        super().__init__(cpu, ram)
        self.battery = battery

    def move(self):
        print('노트북은 이동 가능해요')


laptop = Laptop('2core', '4GB', '100%')
print(laptop.cpu)
laptop.move()

위와 같은 관계를 IS-A라고 부른다! ~은 ~의 한종류다.

+ Recent posts