C++与Java在一些语言特性上的异同

来源:互联网 发布:深圳什么网络好 编辑:程序博客网 时间:2024/05/17 12:54

  引言:据说程序员面试时会问到C++与Java的异同,而比较本身就有助于加深印象。我从两本书中作了一些摘录,更高级的语言特性还有待朋友们的补充。

C++Java在如下一些语言特性上的异同

1. 访问修饰符(private, public, protected);

C++ NOTE

As it happens, protected features in Java are visible to all subclasses as well as to all other classes in the same package. This is slightly different from the C++ meaning of protected, and it makes the notion of protected in Java even less safe than in C++.

Here is a summary of the four access modifiers in Java that control visibility:

Visible to the class only (private).

Visible to the world (public).

Visible to the package and all subclasses (protected).

Visible to the package—the (unfortunate) default. No modifiers are needed.

(摘自Classes, Superclasses, and Subclasses, Chapter 5, Inheritance, Core Java™ 2 Volume I - Fundamentals, Seventh Edition, By Cay S. Horstmann, Gary Cornell, Publisher: Prentice Hall PTR, Pub Date: August 17, 2004

2. 虚类/抽象类Abstract Class

For added clarity, a class with one or more abstract methods must itself be declared abstract.

In addition to abstract methods, abstract classes can have concrete data and methods.

A class can even be declared as abstract even though it has no abstract methods.

Abstract classes cannot be instantiated.

Note that you can still create object variables of an abstract class, but such a variable must refer to an object of a nonabstract subclass.

C++ NOTE

In C++, an abstract method is called a pure virtual function and is tagged with a trailing = 0.

A C++ class is abstract if it has at least one pure virtual function. In C++, there is no special keyword to denote abstract classes.

(摘自Abstract Classes, Chapter 5, Inheritance, Core Java™ 2 Volume I - Fundamentals, Seventh Edition

3. 接口

接口类似于Java中的类,只是其中仅仅提供抽象的方法。开发人员不能实例化一个抽象的类。接口的目的是强制任何子类实现其中的方法,使其他类能够利用子类中实现的方法。

接口的功能同类一样,但并不要求必须存在一种父子关系。如同类和数组一样,接口也是引用类型。接口类型可以用做形式参数,具有同样功能的类将用做实际参数。类与接口的关联关系是,如果一个类实现了某个接口,则这个类必须提供接口中规定的所有方法。

接口不提供任何具体的行为,提供的仅仅是其承诺的行为的说明。下面是接口成员应具有的属性定义,不管开发人员是否提供了相应的修饰符:

●接口的成员(包括字段和方法)总是具有public属性,即使没有定义也是如此;

●接口中的数据字段具有final属性,即使没有定义也是如此;

●接口中的方法具有abstract属性,即使没有定义也是如此。

使用接口的根本原因是把某些类拥有的某些行为从使用这些行为的类中分离出来,使编译程序也能够帮助检查。

(摘自第11章,接口,《Java 2 教程(第六版)》,[]Peter van der Linden,电子工业出版社)

 

Other programming languages, in particular C++, allow a class to have more than one superclass. This feature is called multiple inheritance. The designers of Java chose not to support multiple inheritance, because it makes the language either very complex (as in C++) or less efficient (as in Eiffel).

Instead, interfaces afford most of the benefits of multiple inheritance while avoiding the complexities and inefficiencies.

C++ NOTE

C++ has multiple inheritance and all the complications that come with it, such as virtual base classes, dominance rules, and transverse pointer casts. Few C++ programmers use multiple inheritance, and some say it should never be used. Other programmers recommend using multiple inheritance only for "mix in" style inheritance. In the mix-in style, a primary base class describes the parent object, and additional base classes (the so-called mix-ins) may supply auxiliary characteristics. That style is similar to a Java class with a single base class and additional interfaces. However, in C++, mix-ins can add default behavior, whereas Java interfaces cannot.

(摘自Interfaces, Chapter 6, Interfaces and Inner Classes, Core Java™ 2 Volume I - Fundamentals, Seventh Edition