Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active January 23, 2021 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nattybear/ec6411758b8ebf94892924890dcde6f0 to your computer and use it in GitHub Desktop.
Save nattybear/ec6411758b8ebf94892924890dcde6f0 to your computer and use it in GitHub Desktop.

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

Creating Your Own Classes

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

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

  • 인스턴스 변수
  • 메소드
  • 클래스 변수
  • 클래스 메소드

클래스 변수는 인스턴스의 상태가 아니라 클래스의 상태를 갖는 것이다.

클래스 메소드는 인스턴스가 실행하는 것이 아니라 클래스가 실행하는 것이다.

아마 다른 언어의 static 개념인 것 같다.

아래 목록들은 필수가 아니라 옵션이다. 적지 않아도 된다. 그런데 주석이나 카테고리 설명을 적는 것은 좋은 습관이다.

  • 클래스 변수
  • 클래스 메소드
  • 주석
  • 카테고리
SuperclassName subclass: SubclassName [
  ...
]

대괄호 열기 전까지를 클래스의 헤더 header라고 부른다.

대괄호 안의 영역을 클래스의 바디 body 라고 부른다.

클래스 메소드 헤더는 아래처럼 작성한다.

SubclassName class >> aClassMethod: aParameter

메소드 안에서 리턴할 때는 아래와 같이 ^를 사용한다.

^objectToReturn

리턴은 할 필요가 없을 때는 안 해도 되지만 명시적으로 적지 않은 경우에는 메소드 안에서 마지막으로 평가한 표현식을 리턴한다.

그렇더라도 명시적으로 적는 것이 좋은 습관이다.

인스턴스 메소드는 아래와 같이 만든다.

anInstanceMethod [
  "Comment to describe this instance method"
  <category: 'Category of this instance method'>
  
  | localVariable1 localVariable2 |
  
  ...
  
  ^objectToReturn
]

Computer Programming with GNU Smalltalk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment