Smalltalk和Java中的OO有哪些主要区别? [英] What are the key differences between OO in Smalltalk and Java?

查看:177
本文介绍了Smalltalk和Java中的OO有哪些主要区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Smalltalk和Java中的OO有哪些主要区别?

What are the key differences between OO in Smalltalk and Java?

请注意,我是一名Java程序员,试图通过探索Smalltalk来扩展他的视野。目前我对Smalltalk几乎一无所知,只不过它比Java更纯净。因此,我更喜欢这个答案,它显示了各种Java概念如何映射到相应的Smalltalk概念,然后介绍了Java中根本不存在的Smalltalk概念。

Please note that I am a Java programmer trying to expand his horizons by exploring Smalltalk. Currently I know almost nothing about Smalltalk except that it's purer than Java. Therefore I'll prefer the answer that shows how various Java concepts map to corresponding Smalltalk concepts and then introduces the Smalltalk concepts that don't exist in Java at all.

推荐答案

消息传递



Smalltalk使用消息传递,而不是方法调用。区别是微妙的,但非常强大。

Message passing

Smalltalk uses message passing, not method invocation. The distinction is subtle, but enormously powerful.

一些术语:给定 foo bar:baz #bar:是一个选择器,foo是一个名为的消息的 receiver #bar:(#表示符号,就像Common Lisp会说'bar (或者更合适的是,:bar ))和 baz 参数参数。当行执行时, foo 发送消息#:bar:,参数 baz 。到目前为止,这很正常。在Java中它看起来像 foo.bar(baz);

Some terminology: Given foo bar: baz, #bar: is a selector, foo is the receiver of a message called #bar: (the # indicates a symbol, much like Common Lisp would say 'bar (or even more appropriately, :bar)), and baz is an argument or parameter. When the line's executed, foo is sent the message #:bar: with argument baz. So far, it's pretty normal. In Java it would look like foo.bar(baz);.

在Java中,运行时系统会计算out foo 的实际类型,找到最合适的方法,并运行它。

In Java, the runtime system would figure out foo's actual type, find the most appropriate method, and run it.

事情看起来在Smalltalk中差不多相同。向对象发送消息时,它会在其方法字典中搜索名称与消息选择器名称匹配的方法。如果找不到,则在其超类'方法字典中搜索,依此类推。很正常的东西。

Things look almost the same in Smalltalk. When you send an object a message, it searches in its method dictionary for a method whose name matches that of the selector of the message. If it can't find one, it searches in its superclass' method dictionary, and so on. Pretty normal stuff.

如果找不到任何匹配的方法,它会自己发送 #doesNotUnderstand:消息,将原始消息作为参数。 (是的,消息发送是一个对象。)但 #doesNotUnderstand:也只是一种方法。你可以覆盖它。

If it can't find any matching method, it sends itself the #doesNotUnderstand: message, with the original message as a parameter. (Yes, a message send is an object.) But #doesNotUnderstand: is also just a method. You can override it.

例如,你可以有一个对象响应某些消息集,同时将它收到的任何其他消息转发给某个委托对象。覆盖 #doesNotUnderstand:并且嘿presto,你有一个不需要维护的代理,以使其协议与代表保持同步。

For instance, you can have an object that responds to some set of messages while forwarding any other messages it receives to some delegate object. Override #doesNotUnderstand: and hey presto, you have a proxy that will need no maintenance to keep its protocol in sync with the delegate.

不,我不是在开玩笑。 Smalltalk的整个语法可能长达15行。 JLS是......不是。为何关心?简单的语法使得分解大量代码变得简单。元编程!重构!

No, I'm not joking. Smalltalk's entire grammar's maybe 15 lines long. The JLS is... not. Why care? A simple syntax makes it simple to tear a chunk of code apart. Metaprogramming! Refactoring!

没有语法:


  • 条件陈述:(n< 3)ifTrue:['yes'] ifFalse:['no']

  • for loops: 1到:10做:[:i |成绩单显示:i asString]

  • try-catch: [i:= i / 0] ifError:['oops!']

  • try-finally: [i:= i / 0]确保:[stream close]

  • conditional statements: (n < 3) ifTrue: ['yes'] ifFalse: ['no']
  • for loops: 1 to: 10 do: [:i | Transcript show: i asString]
  • try-catch: [i := i / 0] ifError: ['oops!']
  • try-finally: [i := i / 0] ensure: [stream close]

并注意所有那些 [] s - 干净的头等舱关闭语法。

And notice all those []s - first-class closures with a clean syntax.

这篇关于Smalltalk和Java中的OO有哪些主要区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆