상세 컨텐츠

본문 제목

What is delegation?

Python

by techbard 2016. 5. 9. 11:56

본문

반응형

# Delegation


"""

객체 B가 객체 A의 특화 버전이라고 한다면


- B는 A의 모든 특성을 가지고 있다.

- B는 A에는 없는 새로운 특성을 가지고 있다.

- B는 A와 공통으로 가진 특성에 대해 다른 식으로 동작할 수 있다.


* Delegation을 구현하는 두 가지 방법

1. inheritance

2. composition


- 1은 커플링의 문제가 있음

- 1은 부모 객체를 그대로 복사함

"""


class Output():

def __init__(self, s):

self.s = s


def printit(self):

print(self.s)


def print_delim(self):

print("=====" * 5)


class O():

def __init__(self, s):

self.output = Output(s)


def printit(self):

print(self.s.upper())


def __getattr__(self, attr):

return getattr(self.output, attr)


def print_parent(self):

print(self.output.s)


o1 = O("hello")

o1.printit()

o1.print_delim()

o1.print_parent()


# 결과

HELLO

=========================

hello




반응형

관련글 더보기

댓글 영역