Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nattybear
Last active January 23, 2021 02:15
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/9cd4d9dfaad4ceeebc489c35d1cec67b to your computer and use it in GitHub Desktop.
Save nattybear/9cd4d9dfaad4ceeebc489c35d1cec67b to your computer and use it in GitHub Desktop.

이 글은 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 |

  animalNumber := 0.

  <comment: 'A class for defining animals.'>

  Animal class >> setAnimalNumber: number [
    "A class method to set the animal number."
    <category: 'accessing'>

    animalNumber := number.

    ^animalNumber
  ]

  Animal class >> getAnimalNumber [
    "A class method to get the animal number."
    <category: 'accessing'>

    ^animalNumber
  ]

  setName: newName [
    "An instance method to set the animal's name."
    <category: 'accessing'>

    name := newName.
  ]

  getName [
    "An instance method to get the animal's name."
    <category: 'accessing'>

    ^name
  ]
]

Animal subclass: Dog [
  <comment: 'A dog class.'>

  makeNoise [
    "An instance method to get the dog's noise."
    <category: 'accessing'>

    'Woof!' printNl.
  ]
]

Animal subclass: Cat [
  <comment: 'A cat class.'>

  makeNoise [
    "An instance method to get the cat's noise."
    <category: 'accessing'>

    'Miaow!' printNl.
  ]
]

dog := Dog new.
Animal setAnimalNumber: 1.
dog setName: 'Karabash'.
dog getName printNl.
dog makeNoise.

cat := Cat new.
Animal setAnimalNumber: 2.
cat setName: 'Minnosh'.
cat getName printNl.
cat makeNoise.

클래스 Object는 모든 클래스의 superclass이다. new 같이 미리 정의되어 있는 메소드를 가지고 있다.

Computer Programming with GNU Smalltalk

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