Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
942 views
in Technique[技术] by (71.8m points)

oop - What is the Smalltalk equivalent of Java's static?

What is the Smalltalk equivalent of Java's static fields and methods? IOW, what do the Smalltalkers do when they need class level data and/or methods?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We use class-side methods/instance variables. A class is an object, after all, so can have methods.

For instance, the Rectangle class has a method #origin:corner: so you may write

Rectangle origin: 0@0 corner: 100@100

to create a Rectangle. This is just the message #origin:corner: sent to the object called Rectangle (a class is an object!) with the two Points as parameters.

Class-side instance variables work much the same way. A class, being an object, may have instance variables. From the SUnit library:

TestCase class
  instanceVariableNames: 'history'

and then TestCase class exposes this in the usual way, with a getter/setter (#history and #history:).

EDIT: The @ I used has caused a fair bit of discussion. It's what's called a binary message, which allows one to define selectors that look just what other languages would call infix operators. For instance, 3 + 4, or 0@0. In the case of @, the Number class defines a method called @ taking a parameter y, defined as ^Point x: self y: y - "return a Point whose x coordinate is my own value and whose y coordinate is the parameter".

Point is an ordered pair, but of course there's nothing stopping one from defining higher-dimensional versions. Point might define a method called @ that looked like this, for instance: ^Triple x: self x y: self y z: z - "return a point in R^3 whose x, y coordinates are my own, and whose z coordinate is the given parameter".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...