Closed cwdoh closed 7 years ago
TODO: class delegation의 다른 형태가 뭔가 있을 수도 있겠다. 고민해보자
글 잘 보고있습니다.^^
Class delegation에서 요약해주신 글이요. 원문과 요약문이 잘 매칭이 안되서요..
아래의 형태가 맞지 않을까요? A = Base, B = BaseImpl, b = BaseImpl의 object, C = Drived 라고 이해했습니다.
@elvin-han 말씀하신 바가 맞습니다. 제가 설명을 수정하던 중에 알파벳을 잘못 바꿨군요. ㅜ C 내의 private member로 b가 저장되고, A 메소드는 이를 참조하는 형태로 C에 동일하게 생성되는 것이 맞습니다.
23 에서 궁금했던 by에 대해 역시 의식의 흐름대로...
Class Delegation
warmup
간단하게 interface를 정의하고 이를 기반으로 코드를 구현
by
에 의한 메소드 생성문서에 보면 아래와 같은 내용이 있다.
요약하면,
interface A
class B : A
b = B(x)
가 있을 때class C : A by b
형태의 구현은위의 코드는 아래와 같이 구현된다.
$$delegate_0
로 Base가 포함된다printX()
메소드가 생성되며, 이는 다시 Base$$delegate_0
에 동일 메소드를 호출한다.interface에 없는 메소드를 구현하면?
생성된 코드를 보면 이전과 같다.
Delegated properties
문법
val/var <property name>: <Type> by <expression>
요약하면, prop의 get와 set을 getValue()와 setValue()로 위임한다. 인터페이스가 필요없다.
23 에서 확인했던 lazy에서의 내부구조와 크게 다를 것은 없다.