1、smalltalk超简明教程Smalltalk 超简明教程Simberson 公司的David Buck 撰写的此文档是为C/C+/C#/Java 程序员介绍Smalltalk 的,而且已经得到他善意的许可而得以发布。如果你对正式的培训有兴趣请联系Simberson,咨询Smalltalk 培训。我听说很多Java 和C#程序员说他们看不懂Smalltalk 代码。Smalltalk 因为只有很少的语法,所以实际上它非常的简单。Smalltalk 的强大之处是它的环境和类库。本文意图通过代码对比,使Java 和C#程序员迅速理解Smalltalk。Copyright David Buck 20
2、05 临时变量Smalltalk 不需要变量的类型声明。临时变量通过两个竖线来定义JavaSmalltalkint a;char b;float c;| a b c | 赋值Smalltalk 使用 := 赋值JavaSmalltalka = 5;a := 5 消息Smalltalk 有三种消息类型形式参数例子一元运算符以小写字母开始的字符和数字0squared二元运算符连接符1+关键字使用多个冒号终结文字和数字字符1或者多个do:,between: and:传输一个或多个参数时,需要使用关键字消息。每一个参数前面需要添加关键字。Smalltalk不使用和;分割参数。例子:JavaSmallt
3、alk(); (10);(20,anotherAccount) (anotherAccount);myAccount getBalancemyAccount setBalance: 10myAccount transfer: 20 to:anotherAccountmyAccount = anotherAccount 运算优先级:一元运算符 (首先运算)二元运算符 (其次运算)关键字 (最后运算)在同等优先级下,运算是从左到右JavaSmalltalk3 + 5 * 6 myAccount transfer: 20 to:anotherAccount 常量Smalltalk 中整数类型,字符类
4、型,字符串类型,布尔类型,浮点数类型和双精度浮点数类型都是头等类对象。整数类型是无限精度的,会按需自动增长,而不会溢出(译者注:跟内存大小有关)。实际上,Smalltalk 中没有char 类型,byte 类型,short 类型,int 类型或者long 类型的相对应的类型。它们都是整数类型。JavaSmalltalk55012308r12300x7f16r7f3r21012 (你可以选择你想用的基数)200L2e-52e-52e-5d2d-5h$hu03A9Character value: 16r3A9hellohellocantcant 特定词Smalltalk 中,nil 引用一个真实的
5、对象,它是UndefinedObject 类的实例。true 是True类的实例,flase 是Flase 类的实例JavaSmalltalkthisselfnullniltruetruefalsefalsesuperbase (C#)super 方法返回JavaSmalltalkreturn value;value 级联JavaSmalltalkmyAccountdeposit: 20;transfer: 20 to: anotherAccount 注释JavaSmalltalk/* comment */etTime();endTime = new GregorianCalendar().g
6、etTime();Reservation class methods:newsuper new initializeReservation instance method:initializestartTime := Timestamp now.endTime := Timestamp now 块Smalltalk 称对象为块,它是包含可执行代码的对象。Java 中最相似的东西是匿名内部类,C# 中,跟匿名托管相类似。执行“块”无需参数,可以向它发送一个value 消息Smalltalk| block |block := 3 + 4.block value answer is 7块可以有参数,
7、每一个块参数的声明必须以冒号(:)开头,以竖线(|)表示参数列表的结束和块代码的开始。Smalltalk| block |block := :x :y | x * 2 + y.block value: 5 value: 3 answer is 13 语法结束到此为止,我们已经介绍了Smalltalk 所有的语法。其余的事情属于类库部分。你是否注意到我们是否忘记了某些事情if-then-else 或者while 循环Smalltalk 只使用块和普通的消息发送。 控制结构Smalltalk 没有在语言中内置控制结构。Smalltalk 使用向true 或者flase 对象发生消息作为替代。Jav
8、aSmalltalkif (tries 5)return Too many tries;elsereturn Trying again;tries 5ifTrue: Too many triesifFalse: Trying again注意表示从方法返回,不只是适用于块。 循环Smalltalk 使用块做循环。由于块是对象,所有我们可以先他们发生消息JavaSmalltalkint tries = 0;while (tries = 5) tryAgain();tries+;| tries |tries := 0.tries 500000.d := clients collect: :clien
9、t | client name.In the first case, we get a subcollection of those clients that are pretty rich. In the second, we get a collection of names (the original collection was a collection of clients).序列化抽象无须创建新的classEvery now and then, a reader will find code that looks likestuff value: x value: y value:
10、 zwhere the keywords are all “value:”. This clearly looks useless and bewildering to a non-Smalltalk reader. What is happening is that the programmer has (typically) created a new abstraction facility.Let me explain this fundamental capability that only Smalltalk supports with a challenge. Given tha
11、t we have already introduced the Client class earlier, suppose that we want a simple facility for looping through an individual clients parts; ., we want the loop to first give us the name, then the next time around, we want the age, and finally we want the address.The conventional solution in C+ an
12、d Java is to create a special stream class or enumerator, say called ClientIterator, with facilities to initialize it, to ask it if it is at the end, and, if not, to ask for the next object in the iterator. Then we can have a loop that sets up the iterator and, while not at the end, gets the next ob
13、ject and processes it. The advantage of the iterator is that it can provide its own variables for keeping track of where its at in the sequencing process; ., its not necessary to extend the Client class with “temporary” instance variables needed for iterating.An example piece of code that might make use of
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1