Java,Perl,Python 比较

来源:互联网 发布:linux 复制全部内容 编辑:程序博客网 时间:2024/05/17 23:41

近期打算好好学习Perl,Python和Java,可是Java只了解这些纯java的东西,什么EJB之类的不在兴趣范围之类,就跳过了。一直在想需要从哪些方面进行比较学习,网上刚好找到几篇英文的比较的。先把我感兴趣的写在下面,在后面的文章中就按照这个思路慢慢学习吧。(有些内容可能不一定对,望有人能帮忙更正)



Java

Python

Perl

Object-Orientation

Hybrid

Hybrid

Add-On / Hybrid

Static / Dynamic Typing

Static

Dynamic

Dynamic

Generic Classes

Java从1.5开始支持

N/A

N/A

Inheritance

Single class, multiple interfaces

Multiple

Multiple

Feature Renaming

No

No

No

Method Overloading

Yes

No

No

Operator Overloading

No

Yes

Yes

Higher Order Functions

No

Lambda Expressions

Yes (???)

Lexical Closures

No

Yes (since 2.1)

Yes

Garbage Collection

Mark and Sweep or Generational

Reference Counting

Reference Counting

Uniform Access

No

No

No

Class Variables / Methods

Yes

No

No

Reflection

Yes

Yes

Yes?

Access Control

public, protected, "package", private

Name Mangling

None

Design by Contract

No

No

No

Multithreading

Yes

Yes

Yes

Regular Expressions

Standard Library

Standard Library

Built-in

Pointer Arithmetic

No

No

No

Language Integration

C, some C++

C, C++, Java

C, C++

Built-In Security

Yes

No?

Yes (perlsec)

Capers Jones Language Level*

6

N/A

15

下面从每一个类别细说分别表示什么意思。

1.Object-Orientation:

这里所说的面向对象主要从以下五个方面考虑:



Java

Python

Perl

Encapsulation / Information Hiding

Yes

No

Yes?

Inheritance

Yes

Yes

Yes?

Polymorphism / Dynamic Binding

Yes

Yes

Yes?

All pre-defined types are Objects

No

Yes

NO

All operations are messages to Objects

No

NO

NO

All user-defined types are Objects

Yes

Yes

NO

Java宣称是纯面向对象的,但是他包含一些不是对象的基本类型,同时他也实现了一些build-in的基本算术操作符,因此并不是所有的操作都是消息。
而Python也被看做是面向对象的语言,但是它的一些操作以方法的形式而另一些则是以全局函数的形式实现,同时在方法中需要显示传入self参数也不被看好,而其在数据的私有和隐藏属性方面也是褒贬不一。
Perl应该是面向过程的语言,但支持一些面向对象的属性。

2 动态、静态类型:

动态和静态类型是一个争论已久的问题,动态类型的支持者认为动态更加灵活同时生产率也更高,而静态类型的支持者则认为静态更高效也更安全。

动态类型是说一个变量不需要申明特定的类型,任何变量都可以存放任何类型或者对象。它保证了语言更为灵活也易于变更。但如果一个变量存放的不是用户所期望的类型的其他类型的时候,会出现一些运行时的错误。

静态类型要求变量必须申明其特定的类型,编译器会保证任何变量都得到的是其所包含的特定类型。


3 Generic Classes(应该就是指的泛型类吧)

Generic classes, and more generally parametric type facilities, refer to the ability to parameterize a class with specific data types. A common example is a stack class that is parameterized by the type of elements it contains. This allows the stack to simultaneously be compile-time type safe and yet generic enough to handle any type of elements.

The primary benefit of parameterized types is that it allows statically typed languages to retain their compile-time type safety yet remain nearly as flexible as dynamically typed languages. Eiffel in particular uses generics extensively as a mechanism for type safe generic containers and algorithms. C++ templates are even more flexible, having many uses apart from simple generic containers, but also much more complex.

As already mentioned in the previous section, Java's lack of generic classes is a severe hole in the Java type system. When one considers that most living objects in a program are stored in container classes, and that containers in Java are untyped due to lack of generics, it is questionable whether Java's type system provides any benefit over the more flexible dynamic counterparts. See also this article by Dave Thomas for a discussion of Java's type system in regards to its lack of generics.

Dynamically typed languages do not need parameterized types in order to support generic programming. Types are checked at run-time and thus dynamically typed languages support generic programming inherently.


Feature Renaming( 不是很懂,因为没用到过,说是ruby有这个功能,知情者出来解释一下)

Feature renaming is the ability for a class or object to rename one of its features (a term we'll use to collectively refer to attributes and methods) that it inherited from a super class. There are two important ways in which this can be put to use:

    * Provide a feature with a more natural name for its new context
    * Resolve naming ambiguities when a name is inherited from multiple inheritance paths

As an example of the first use, consider again a stack implemented by inheriting from an array. The array might provide an operation called remove_last to remove the last element of the array. In the stack, this operation is more appropriately named pop.


Design by Contract:(契约设计)
话说这一个我真的不太懂说的是啥,网上找了资料还是不太明白,先搁着,有人懂得可以用通俗的方法给我说明一下,很是感谢。
DBC最早是有Bertrand Meyer 的 Eiffel programming language提出。DBC在Evans DDD的柔性设计中也谈到了。所以,DDD是集OO设计大成,正因为它是一个总结,你就不能把它和其他思想并列在一起,这有上下层次之分,我在这里强调DDD了,就是排斥其他思想,这是很多初学者外行看热闹的误解。

Contract契约是对Object对象行为的描述表达,也就是说:一个对象的行为方法做到什么样为合适?这是有数据库背景的人转到OO上一个鸿沟,而通过契约来约定是一种主要的设计方法。

http://en.wikipedia.org/wiki/Design_by_contract

DBC分三种:
1.Post-conditions 后置条件postcondition 表示调用一个方法一定会得到的结果。类似断言Assertion,如果语言不支持断言,那么我们就必须自己写断言,也就是测试驱动了。

2.Pre-conditions 前置条件precondition ,预先保证后置条件必须满足前置条件。
前置条件必须满足,后置条件必须实现,通过契约的前置和后置条件的结合,就不会出现有隐藏的功能obligations,这样,事情清清楚楚地被摆出来。这样设计才能落实为代码,保证正常的对象调用。

3.类不变量class invariant 表示对象状态的断言,执行完任何操作后都都应该被满足,不变量还是对聚合体进行完整性严格定义。

不变性意义非常重要,不变性意思是对象的所有属性必须由其方法来保证一致性。DDD推广到根对象要通过方法保证其聚合体内所有对象的属性保持一致性。比如 ForumThread的addMessage就保证加入新的Message以后,其聚合体内相关属性一致修改,保持不变性是一个排他性过程,或者说需要由锁或事务来完成,这又和伸缩性scalable设计有关。




6正则表达式的支持:Perl语言本身支持正则表达式,而Pthon和Java通过标准库来支持。

7 Pointer Arithmetic
直接操作内存的权利。很多语言可以通过和C集成来操作内存,但语言本身不支持。

8 Language Integration
同其他语言集成的能力。

9.Build-In Security:
指语言的实现能不能发现一段代码是否来自可信赖的源。如果不是能否限制其权限。比如Java的Applet被看做是不可信任的,在用户的浏览器里面执行的时候起可以操作的行为也是有限的。比如,不能读写用户的硬盘,除了originating host以外不能打开其他的网络连接等。

10:Capers Jones Language Level

实现同一功能所需要的代码的行数。