Example Program
  1. "Smalltalk Example Program"
  2. "The following is a class definition, instantiations
  3. of which can draw equilateral polygons of any number of
  4. sides"
  5. class name Polygon
  6. superclass Object
  7. instance variable names ourPen
  8. numSides
  9. sideLength
  10. "Class methods"
  11. "Create an instance"
  12. new
  13. ^ super new getPen
  14.  
  15. "Get a pen for drawing polygons"
  16. getPen
  17. ourPen <- Pen new defaultNib: 2
  18. "Instance methods"
  19. "Draw a polygon"
  20. draw
  21. numSides timesRepeat: [ourPen go: sideLength;
  22. turn: 360 // numSides]
  23.  
  24. "Set length of sides"
  25. length: len
  26. sideLength <- len
  27.  
  28. "Set number of sides"
  29. sides: num
  30. numSides <- num