面向对象编程语言的特征比较

Many languages claim to be Object-Oriented. While the exact definition of the term is highly variable depending upon who you ask, there are several qualities that most will agree an Object-Oriented language should have:

许多编程语言声称自己是面向对象的(Object-Oriented)。但当询问“面向对象”的确切定义时,不同的人的答案区别很大,下面列举几条大家公认的,面向对象语言所应具有的性质:

  1. Encapsulation/Information Hiding (封装/信息隐藏)
  2. Inheritance (继承)
  3. Polymorphism/Dynamic Binding (多态/动态绑定)
  4. All pre-defined types are Objects (所有预定义类型皆对象)
  5. All operations performed by sending messages to Objects (所有操作都由向对象发送消息实现)
  6. All user-defined types are Objects (所有用户定义的类型都是对象)

A language is considered to be a "pure" Object-Oriented languages if it satisfies all of these qualities. A "hybrid" language may support some of these qualities, but not all. In particular, many languages support the first three qualities, but not the final three.

如果一门编程语言满足了所有这些性质,我们就可以认为这门语言是“纯粹的”面向对象语言。一门“混合型”语言可能支持这些性质中的某几条,但不是全部。特别的,许多语言支持前三条性质,但不支持后三条。

下表列出了几种常见的编程语言对这6条性质的满足情况对比。

  Eiffel Smalltalk Ruby Java C# C++ Python Perl Visual Basic
Encapsulation / Information Hiding Yes Yes Yes Yes Yes Yes No Yes? Yes?
Inheritance Yes Yes Yes Yes Yes Yes Yes Yes? No
Polymorphism / Dynamic Binding Yes Yes Yes Yes Yes Yes Yes Yes? Yes (through delegation)
All pre-defined types are Objects Yes Yes Yes No No No Yes No No
All operations are messages to Objects Yes Yes Yes No No No No No No
All user-defined types are Objects Yes Yes Yes Yes Yes No Yes No No

Eiffel, Smalltalk, and Ruby are all pure Object-Oriented languages, supporting all six qualities listed above. Java claims to be a pure Object-Oriented language, but by its inclusion of "basic" types that are not objects, it fails to meet our fourth quality. It fails also to meet quality five by implementing basic arithmetic as built-in operators, rather than messages to objects.

Eiffel, Smalltalk和Ruby都是纯的面向对象语言,支持上表列举的所有6条性质。Java声称自己是纯粹的面向对象语言,但是由于其“基本”数据类型并不是对象,它不满足我们的第4条性质。它也不满足第5条性质,因为其基本的算术运算是内置的运算符,而不是对象的消息。

C++ is considered to be a multi-paradigm language, of which one paradigm it supports is Object-Orientation. Thus, C++ is not (nor does it contend to be) a pure Object-Oriented language.

C++被认为是一种多范式语言,它支持的范式之一是面向对象的。因而,C++不是(它也不满足于)一门纯粹的面向对象语言。

Python is often heralded as an Object-Oriented language, but its support for Object-Orientation seems to have been tacked on. Some operations are implemented as methods, while others are implemented as global functions. Also, the need for an explicit "self" parameter for methods is awkward. Some complain about Python's lack of "private" or "hidden" attributes, which goes against the Encapsulation/Information Hiding principle, while others feel that Python's "privateness is by convention" approach offers all of the practical benefits as language-enforced encapsulation without the hassle. The Ruby language, on the other hand, was created in part as a reaction to Python. The designer of Ruby decided that he wanted something "more powerful than Perl, and more Object-Oriented than Python." You can see this comparison of Python and Ruby for more information.

Python经常被标榜为一种面向对象语言,但它对面向对象的支持似乎是被“贴上去的”。一些操作以方法的形式实现,而另一些则用的是全局函数。另外,方法参数中需要显式地添加“self”参数也很令人尴尬。有一些人抱怨Python缺乏“private”或者“隐藏”属性,这违背了封装/信息隐藏的原则,而另一些人则认为“通过约定实现的私有”在不增加额外限制的同时,也提供了语言强制的封装带来的优势。在另一方面,Ruby语言的发明一部分是由于Python。Ruby的设计者认为他想要创造一种“比Perl强大,比Python更面向对象”的语言。你可以从这里的链接了解更多关于Python和Ruby的比较信息。http://dev.rubycentral.com/faq/rubyfaq-2.html

Visual Basic and Perl are both procedural languages that have had some Object-Oriented support added on as the languages have matured.

Visual Baisc和Perl都是过程化的语言,随着语言的发展成熟,增加了一些面向对象的支持。

本文节选、翻译自博文:http://www.jvoegele.com/software/langcomp.html

本文链接:http://bookshadow.com/weblog/2014/11/28/object-oriented-programming-language-comparison/
请尊重作者的劳动成果,转载请注明出处!书影博客保留对文章的所有权利。

如果您喜欢这篇博文,欢迎您捐赠书影博客: ,查看支付宝二维码

Pingbacks已关闭。

暂无评论

张贴您的评论