Skip to content

Instantly share code, notes, and snippets.

View nattybear's full-sized avatar

Joonkyu Park nattybear

View GitHub Profile

Extending and modifying classes

기존 클래스에 새로운 메소드를 추가하고 싶을 때는 아래와 같이 extend를 사용한다.

className extend [
  | newInstanceVariable1 newInstanceVariable2 |
  
  methodSelector [
    ...

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Example

"animal.st"
"A program which creates some animal classes to illustrate the object oriented concepts."

Object subclass: Animal [
  | name |

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Creating Objects from Classes

생성한 클래스의 객체를 만들 때는 아래와 같이 한다.

ourObjectName := SubclassName new

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Creating Your Own Classes

사용자가 만든 클래스를 커스텀 클래스 custom class라고 한다.

클래스 내부에는 아래와 같은 것들을 정의할 수 있다.

  • 인스턴스 변수
  • 메소드

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Polymorphism

서로 다른 두 객체가 동일한 메세지에 같은 방식으로 대답을 할 수 있다.

그래서 어떤 언어에서는 서로 다른 클래스가 같은 이름의 셀렉터를 써서 메세지를 정의할 수 있다.

또는 subclass가 상속 받은 셀렉터의 기능을 변경할 수도 있다. overriding

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Inheritance

  • 상위 클래스로부터 파생된 클래스를 subclass라고 부른다.
  • 상위 클래스를 superclass라고 부른다.
  • subclass는 superclass의 모든 속성과 행위를 상속 inherit 받는다.
  • 하나의 클래스가 여러 개의 subclass를 가질 수 있지만 여러 개의 superclass를 가질 수는 없다.

객체 간에는 두가지 종류의 관계가 있다.

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Encapsulation

  • 스몰토크에서는 객체의 내부 상태를 직접 수정할 수 없다. 전부 private인가...
  • 메세지를 통해서만 상태를 수정할 수 있다.

Computer Programming with GNU Smalltalk

Review Questions

  1. Write a program which controls if a given number is prime or not and displays the result. The program should continue asking new numbers until number -1 is encountered.

이 글은 Canol Gokel님이 만든 Computer Programming using GNU Smalltalk를 읽고 정리한 것이다.

Repetitive Controlling

whileTrue:

aBlock whileTrue: anotherBlock

Selective Controlling

ifTrue:

ifTrue:의 구조는 아래와 같다.

anObject ifTrue: [block-expression-1. block-expression-2]