Smalltalk 和 Java 中的 OO 之间的主要区别是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3102695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:22:24  来源:igfitidea点击:

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

javaoopprogramming-languagessmalltalk

提问by Jim

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

Smalltalk 和 Java 中的 OO 之间的主要区别是什么?

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.

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

回答by Frank Shearar

Message passing

消息传递

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

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

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

一些术语: Given foo bar: baz#bar:是一个选择器, foo 是被调用的消息的接收者#bar:(# 表示一个符号,很像 Common Lisp 会说的'bar(或者更恰当地说,:bar)),并且baz是一个参数参数。当该行执行时,foo发送#:bar:带有参数的消息baz。到目前为止,这很正常。在 Java 中,它看起来像foo.bar(baz);.

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

在 Java 中,运行时系统会找出foo的实际类型,找到最合适的方法,然后运行它。

Things look almostthe 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.

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

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:消息,并将原始消息作为参数。(是的,发送的消息是一个对象。)但#doesNotUnderstand:也只是一个方法。你可以覆盖它。

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.

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

Trivial syntax

简单的语法

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!

不,我不是在开玩笑。Smalltalk 的整个语法大概有 15 行。JLS 是……不是。为什么要关心?一个简单的语法使得撕开一大块代码变得简单。元编程!重构!

No syntax for:

没有语法:

  • 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]
  • 条件语句: (n < 3) ifTrue: ['yes'] ifFalse: ['no']
  • for循环: 1 to: 10 do: [:i | Transcript show: i asString]
  • 试着抓: [i := i / 0] ifError: ['oops!']
  • 最后尝试: [i := i / 0] ensure: [stream close]

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

并注意所有这些[]s - 语法清晰的一流闭包。

回答by Vijay Mathew

  1. Object Model.In Smalltalk every thing, is an object. Java has primitive types like int and float whose representation and behavior are different from complex objects.
  2. Behavior invocation.Behavior of a Smalltalk object is invoked by sending it a message. Java has methods, which are basically function calls, with the destination object being a special first argument called this.
  3. Encapsulation.Smalltalk has strict encapsulation. An object's fields can be exposed only through messages. In contrast, Java allows public fields.
  4. Dynamism.Smalltalk is extremely dynamic. All types are identified at runtime. A class can be introspected and modified at runtime (dynamic meta-programming!). New classes can be created and instantiated at runtime. Java has static type checking along with runtime polymorphism. There is introspection and reflection, but classes and objects cannot be modified from within a running program.
  5. Syntax.Smalltalk do not have a syntax. Instead it has a simple, consistent format for sending messages. Java, like other languages of the C family, has a complex syntax.
  6. Environment.Most Smalltalk implementations provide a complete, standalone, live computing environment with image based persistence. Some of these environments can even be booted on bare metal. The JVM in turn is usually dependent on an underlying operating system for threading, networking etc. Source code must be entered into text files, compiled and explicitly loaded into the JVM for execution.
  1. 对象模型。在 Smalltalk 中,每件事都是一个对象。Java 有像 int 和 float 这样的原始类型,它们的表示和行为不同于复杂对象。
  2. 行为调用。Smalltalk 对象的行为是通过向它发送消息来调用的。Java 有方法,它们基本上是函数调用,目标对象是一个特殊的第一个参数,称为this
  3. 封装。Smalltalk 有严格的封装。对象的字段只能通过消息公开。相比之下,Java 允许公共字段。
  4. 活力。Smalltalk 是非常动态的。所有类型都在运行时识别。可以在运行时内省和修改类(动态元编程!)。可以在运行时创建和实例化新类。Java 具有静态类型检查和运行时多态性。存在内省和反射,但不能从正在运行的程序中修改类和对象。
  5. 句法。Smalltalk 没有语法。相反,它具有用于发送消息的简单、一致的格式。Java 与 C 系列的其他语言一样,具有复杂的语法。
  6. 环境。大多数 Smalltalk 实现提供了一个完整的、独立的、实时计算环境,具有基于图像的持久性。其中一些环境甚至可以在裸机上启动。反过来,JVM 通常依赖于用于线程、网络等的底层操作系统。源代码必须输入到文本文件中,编译并显式加载到 JVM 中以供执行。

回答by ewernli

A key difference between Java and Smalltalk is that Smalltalk has first-class class (no pun intended).

Java 和 Smalltalk 之间的主要区别在于 Smalltalk 拥有一流的课程(没有双关语)。

A class in Smalltalk is an object. The closest thing to staticmethod and variable is then class-side method and variable, as mentionedby Frank Shearer.

Smalltalk 中的类是一个对象。static正如Frank Shearer所提到的,最接近方法和变量的是类端方法和变量。

But this difference is more profound as soon as inheritance is used. In java class-side inheritance does not exists, while it is possible in Smalltalk.

但是,一旦使用继承,这种差异就会更加深刻。在 java 中不存在类端继承,而在 Smalltalk 中是可能的。

If class Ainherits from B, and if you have aand bwhich are instances of Aand B, in Smalltalk, b classinherits from a class. This would not be the case in Java where a getClass()and b getClass()return instances of Class, which are not related with each other.

如果类A的继承B,如果你有ab它们的实例A,并B在Smalltalk中,b class从继承a class。在 Java 中情况并非如此, wherea getClass()b getClass()return 的实例Class彼此不相关。

Let's say now that class Aimplements the singleton pattern: it has a class-side field instanceand an getter method instance. Class Bis another object with its own instancefield. As a consequence, A instanceand B instancewill return different object.

现在假设该类A实现了单例模式:它有一个类端字段instance和一个 getter 方法instance。类B是另一个拥有自己instance领域的对象。因此,A instanceB instance将返回不同的对象。

This is clearly one of the major difference between Smalltalk and Java from a OO standpoint.

从面向对象的角度来看,这显然是 Smalltalk 和 Java 之间的主要区别之一。

Other difference include the existence of metaclasses, extension methods, duck typing vs static typing, reification of doesNotUnderstandand few other things that make coding in Smalltalk or Java completely different.

其他差异包括元类的存在、扩展方法、鸭子类型与静态类型、具体化doesNotUnderstand以及使 Smalltalk 或 Java 中的编码完全不同的其他一些东西。

And of course, Smalltalk has closure which Java still lacks.

当然,Smalltalk 具有 Java 仍然缺乏的闭包。

See also Why doesn't Java allow overriding of static methods ?

另请参阅为什么 Java 不允许覆盖静态方法?

回答by igouy

trying to expand his horizons by exploring Smalltalk

试图通过探索 Smalltalk 来扩大他的视野

If you are activelytrying to explore Smalltalk then you need to know how to read Smalltalk -

如果您正在积极尝试探索 Smalltalk,那么您需要知道如何阅读 Smalltalk -

"I Can Read C++ and Java But I Can't Read Smalltalk" pdf

“我可以阅读 C++ 和 Java,但我无法阅读 Smalltalk” pdf

回答by Chuck

One Smalltalk concept that doesn't exist in Java but has become increasingly popular in recent years is blocks. Blocks are a form of anonymous functions that include the context they were defined in. Importantly, blocks are also objects. Smalltalk actually lacked any kind of built-in if-statement or for-loop or anything like that, but managed to create the same effect just with message-passing and blocks.

Java 中不存在但近年来越来越流行的一个 Smalltalk 概念是块。块是匿名函数的一种形式,包括定义它们的上下文。重要的是,块也是对象。Smalltalk 实际上没有任何内置的if-statement 或for-loop 或类似的东西,但仅通过消息传递和块就设法创建了相同的效果。

object isBig ifTrue: [self runIntoObject:object] 
            ifFalse: [self katamariBall absorbObject:object].

1 to: 10 do: [:number | number print]

回答by Janko Miv?ek

In Smalltalk everything is the object while in Java things like small integers are still not the first class objects. Also, to continue with numbers, in Smalltalk due to its pure OO nature and strong reflective capabilities we never need to care about the number size, like if an integer is small or large and what happens when small integer overflows to large.

在 Smalltalk 中,一切都是对象,而在 Java 中,诸如小整数之类的东西仍然不是第一类对象。此外,继续讨论数字,在 Smalltalk 中,由于其纯 OO 性质和强大的反射能力,我们从不需要关心数字的大小,例如整数是小还是大以及小整数溢出到大时会发生什么。

回答by mathk

When @Janko Miv?ek mean everything he really mean everything. :)

当@Janko Miv?ek 意味着一切时,他真的意味着一切。:)

Even up to message send, what your are doing is creating an object that is the context.

即使是消息发送,您所做的也是创建一个作为上下文的对象。

Also what you don't have in smalltalk is access modifier (private/ protected / public) You don't have package in some Smalltalk implementation and in most Smalltalk implementation package don't have the same semantic than Java.

此外,您在 smalltalk 中没有访问修饰符(私有/受保护/公共)您在某些 Smalltalk 实现中没有包,并且在大多数 Smalltalk 实现包中没有与 Java 相同的语义。

In smalltalk you don't have control structure like for, if, try/catch... The cool things is that you don't need them because you have block closure in smalltalk.

在 smalltalk 中,你没有像 for、if、try/catch 这样的控制结构……很酷的事情是你不需要它们,因为你在 smalltalk 中有块闭包。

In smalltalk you don't have static member instead you have Class that are object(you can send message to class, you also can hold class in a variable).

在 smalltalk 中,您没有静态成员,而是拥有对象类(您可以向类发送消息,也可以将类保存在变量中)。

In smalltalk you don't have nested class.

在 smalltalk 中,您没有嵌套类。

...

...