SqueakByExample:12.2: Difference between revisions

From 흡혈양파의 번역工房
Jump to navigation Jump to search
mNo edit summary
(번역수정)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
==스몰토크 오브젝트 모델을 다시 살펴보기==
==스몰토크 객체 모델에 대한 복습==
 
모든 것이 객체가 되기 때문에, 스몰토크에서는 "파랑"색상 또한 객체 입니다.


모든 것이 오브젝트 이기 대문에 스몰토크에서 color blue 또한 오브젝트 입니다.
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
Color blue -> Color blue
Color blue -> Color blue
</syntaxhighlight>
</syntaxhighlight>


모든 오브젝트는 클래스의 인스턴스입니다. Color blue의 클래스는 class Color:입니다.  
 
모든 객체 클래스의 인스턴스입니다. "파랑"색상 의 클래스는 Color 입니다.  
 
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
Color blue class → Color
Color blue class → Color
</syntaxhighlight>
</syntaxhighlight>


흥미롭게도, 여러분은 color의 알파 값(the alpha value)을 설정하며, TranslucentColor:인, 다양한 클래스(different class)의 인스턴스를 얻습니다.  
 
흥미로운 사실이 있습니다만, 사용자는 투명도<sup>alpha value</sup>를 지정하면, TranslucentColor 라는 클래스의 인스턴스를 얻을 수 있습니다.
 
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
(Color blue alpha: 0.4) class → TranslucentColor
(Color blue alpha: 0.4) class → TranslucentColor
</syntaxhighlight>
</syntaxhighlight>


우리는 모프(morph)를 만들고 그 색상을 반투명한 색상(translucent color)으로 설정합니다:
 
Morph 를 만들고, Morph 에 대한 색상으로서 반투명 객체 자체를 지정할 수 있습니다:
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
EllipseMorph new color: (Color blue alpha: 0.4); openInWorld
EllipseMorph new color: (Color blue alpha: 0.4); openInWorld
Line 22: Line 28:




여러분은 그림 12.1에 나온 효과(effect)를 볼 수 있습니다.
이렇게하면 그림 12.1 같은 결과가 됩니다.




[[image:TranslucentEllipse.png|none|388px|thumb|그림 12.1: 반투명한 타원]]
[[image:TranslucentEllipse.png|none|388px|thumb|그림 12.1: 반투명 타원]]




Rule 3에 의해, 모든 클래스는 superclass를 갖고 있습니다. TranslucentColor의 superclass는 Color이며, Color의 superclass는 오브젝트입니다:
Rule 3에 의해, 모든 클래스는 super클래스를 가지고 있습니다. TranslucentColor 의 super클래스는 Color 이며, Color 의 super클래스는 Object 입니다:
 
<syntaxhighlight lang="smalltalk">
<syntaxhighlight lang="smalltalk">
TranslucentColor superclass    ⇒    Color
TranslucentColor superclass    ⇒    Color
Line 35: Line 42:




모든 것은 메세지 발송에 의해(Rule 4) 이루어지므로, blue는 Color에 대한 메세지 이며, class 그리고 alpha:는 color blue에 대한 메세지 이며, openInWorld는 타원 모프(ellipse morph)에 대한 메세지고, superclass는 TranslucentColor와 Color에 대한 메세지인 것을 추론할 있습니다. 각 사례의 수신자(receiver)는 오브젝트이며, 그 이유는 모든 것이 오브젝트 이지만, 동시에 오브젝트들의 몇몇은 클래스 이기 때문입니다.  
Rule 4 "모든 동작은 메시지를 보낼 때 일어납니다" 라는 내용을 전제로, blue 는 Color 를 대상으로 하는 메시지, class alpha: 는 "청색"객체 에 대한 메시지, openWorld 는 타원형-Morph 에 대한 메시지, 그리고 superclass 는 TranslucentColor 및 Color 에 대한 메시지가 됩니다. 스몰토크에서 모든것은 객체이기 때문에, 어떤 경우에서도 메시지의 수신자는 객체가 됩니다. 다만 몇몇 경우는 수신자가 클래스인 경우도 있기는 합니다.
 
Rule 5 "메서드 탐색은 상속 관계를 따릅니다" 라는 전제하에, Color blue alpha: 0.4 의 결과로 얻을 있는 객체에 class 메시지를 보내면, 그림 12.2 에서처럼, class 메시지는 작동을 위해 결과 자체를 객체로 취급합니다<ref name="역자주1">Color blue alpha: 0.4 의 PrintIt 결과는 (TranslucentColor r: 0.0 g: 0.0 b: 1.0 alpha: 0.4) 가 됩니다. 또 이 결과 자체에 class 메시지를 보내서 PrintIt 하면 결국은 TranslucentColor 라는 결과를 얻을 수 있습니다</ref>.
 
 
 
아래의 그림은 is-a 의 상속관계를 보여주고 있습니다. 아래 그림의 내용처럼 "Color blue alpha: 0.4" 의 결과로 통해 얻을 수 있는 translucentColor 객체는, 사실 TranslucentColor 클래스의 인스턴스입니다만, 동시에 Color 객체, 또는 Object 의 객체라고 부를수도 있는데, 왜냐하면 translucentColor 객체는 Color 또는 Object 에서 선언된 모든 메시지에도 응답이 가능하기 때문입니다. 사실, isKindOf: 메시지를 객체에 보내면 수신자가 인자로 주어진 클래스와 is-a 의 관계에 있는지를 알 수도 있습니다<ref name="역자주2">isKindOf: 메서드는 Object 클래스에 선언되어있으며 "Answer whether the class, aClass, is a superclass or class of the receiver." 라는 주석을 가지고 있습니다. 메서드를 이용하면 수신자의 클래스가 인자로 주어진 클래스에 대해 상위클래스인지 아닌지를 알아낼 수 있습니다. "2 isKindOf: String" 이런정도의 구문을 쓸 수 있다는 의미가 되겠습니다.</ref>.


메소드 보기(Mehod lookup)는 계층도 사슬(the inheritance chain, Rule 5)을 따르며, 그러므로, 우리가 메세지 클래스(the message class)를 Color blue alpha: 0.4의 결과에 발송하면, 그림 12.2에서 보이는 것 처럼, 클래스 오브젝트에서 대응 메소드(the corresponding method)가 발견될 때, 메세지가 취급(handle)됩니다.


그림은 is-a 관계의 본질을 캡쳐합니다. 우리의 불투명 파랑(blue) 오브젝트는 TranslucentColor 인스턴스이지만, 우리는 또한 그것이 Color 이며, 오브젝트라고 말할 수 있고, 그 이유는 이 불투명 파랑 오브젝트는(blue object) 이 클래스들의 모든 것들에서 정의된 메세지들에 반응하기 때문입니다. 사실, isKindOf: 메세지가 존재하며, 이 isKindOf: 메세지는 여러분이 주어진 클래스와 is a 와의 관계 속에 이 isKindOf:가 있는지를 알아내기 위해 어떤 객체라도 발송할 수 있는 메세지입니다:
[[image:TranslucentClassMessage.png|none|1024px|thumb|그림 12.2: 메시지를 translucent color 에 전송했을때의 상태]]




[[image:TranslucentClassMessage.png|none|1024px|thumb|그림 12.2: 메세지를 불투명 색상(translucent color)에 발송하기]]


==Notes==
==Notes==

Latest revision as of 12:04, 16 September 2013

스몰토크 객체 모델에 대한 복습

모든 것이 객체가 되기 때문에, 스몰토크에서는 "파랑"색상 또한 객체 입니다.

Color blue -> Color blue


모든 객체 클래스의 인스턴스입니다. "파랑"색상 의 클래스는 Color 입니다.

Color blue class  Color


흥미로운 사실이 있습니다만, 사용자는 투명도alpha value를 지정하면, TranslucentColor 라는 클래스의 인스턴스를 얻을 수 있습니다.

(Color blue alpha: 0.4) class  TranslucentColor


Morph 를 만들고, Morph 에 대한 색상으로서 반투명 객체 자체를 지정할 수 있습니다:

EllipseMorph new color: (Color blue alpha: 0.4); openInWorld


이렇게하면 그림 12.1 같은 결과가 됩니다.


그림 12.1: 반투명 타원


Rule 3에 의해, 모든 클래스는 super클래스를 가지고 있습니다. TranslucentColor 의 super클래스는 Color 이며, Color 의 super클래스는 Object 입니다:

TranslucentColor superclass        Color
Color superclass        Object


Rule 4 "모든 동작은 메시지를 보낼 때 일어납니다" 라는 내용을 전제로, blue 는 Color 를 대상으로 하는 메시지, class 와 alpha: 는 "청색"객체 에 대한 메시지, openWorld 는 타원형-Morph 에 대한 메시지, 그리고 superclass 는 TranslucentColor 및 Color 에 대한 메시지가 됩니다. 스몰토크에서 모든것은 객체이기 때문에, 어떤 경우에서도 메시지의 수신자는 객체가 됩니다. 다만 몇몇 경우는 수신자가 클래스인 경우도 있기는 합니다.

Rule 5 "메서드 탐색은 상속 관계를 따릅니다" 라는 전제하에, Color blue alpha: 0.4 의 결과로 얻을 수 있는 객체에 class 메시지를 보내면, 그림 12.2 에서처럼, class 메시지는 작동을 위해 결과 자체를 객체로 취급합니다[1].


아래의 그림은 is-a 의 상속관계를 보여주고 있습니다. 아래 그림의 내용처럼 "Color blue alpha: 0.4" 의 결과로 통해 얻을 수 있는 translucentColor 객체는, 사실 TranslucentColor 클래스의 인스턴스입니다만, 동시에 Color 객체, 또는 Object 의 객체라고 부를수도 있는데, 왜냐하면 translucentColor 객체는 Color 또는 Object 에서 선언된 모든 메시지에도 응답이 가능하기 때문입니다. 사실, isKindOf: 메시지를 객체에 보내면 수신자가 인자로 주어진 클래스와 is-a 의 관계에 있는지를 알 수도 있습니다[2].


그림 12.2: 메시지를 translucent color 에 전송했을때의 상태


Notes

  1. Color blue alpha: 0.4 의 PrintIt 결과는 (TranslucentColor r: 0.0 g: 0.0 b: 1.0 alpha: 0.4) 가 됩니다. 또 이 결과 자체에 class 메시지를 보내서 PrintIt 하면 결국은 TranslucentColor 라는 결과를 얻을 수 있습니다
  2. isKindOf: 메서드는 Object 클래스에 선언되어있으며 "Answer whether the class, aClass, is a superclass or class of the receiver." 라는 주석을 가지고 있습니다. 이 메서드를 이용하면 수신자의 클래스가 인자로 주어진 클래스에 대해 상위클래스인지 아닌지를 알아낼 수 있습니다. "2 isKindOf: String" 이런정도의 구문을 쓸 수 있다는 의미가 되겠습니다.