Class versus Interface Inheritance 类接口继承

来源:互联网 发布:淘宝正品鞋店 虎扑 编辑:程序博客网 时间:2024/05/22 00:14

Class versus Interface Inheritance 类接口继承

It's important to understand the difference between an object's class and its type.

理解对象类和它的类型的不同这是非常重要的

An object's class defines how the object is implemented. The class defines the object's internal state and the implementation of its operations. In contrast, an object's type only refers to its interface—the set of requests to which itcan respond. An object can have many types,and objects of different classes can have the same type.

对象类定义了对象是如何实现的。类定义了对象的内部状态和运算的实现。相比之下,对象的类型只是指它的接口——一套可以对其作出回应的命令。对象有很多类型,不同类的对象可以有同样的类型。

Of course, there's a close relationship between class and type. Because a class defines the operations an object can perform, it also defines the object's type. When we say that an object is an instance of a class, we imply that the object supports the interface defined by the class.

当然,类和类型之间有非常密切的关系。因为类定义了对象可以执行的运算,它也定义了对象的类型。当我们看到类实例化的对象,我们就会明白对象支持的通过类定义的接口。

Languages like C++ and Eiffel use classes to specify both an object's type and its implementation. Smalltalk programs do not declare the types of variables; consequently, the compiler does not checkthat the types of objects assigned to

a variable are subtypes of the variable's type. Sending a message requires checking that the class of the receiver implementsthe message, but it doesn't require checking that the receiver is an instance of a particular class.

C++和eiffel云烟用类指定对象的类型和它的实现。Smalltalk程序不需要声明变量的类型。

因此,编译器对对象分配给类的类型不做检查。发送消息命令检查类接受的实现信息。但是对于收受者是特定类的的实例化不做检查。

It's also important to understand the difference between class inheritance and interface inheritance (or subtyping). Class inheritance defines an object's implementation in terms of another object's implementation. In short, it's amechanism for code and  representation sharing. In contrast, interface inheritance (or subtyping) describes when an object can be used in place of another

理解类继承和接口继承的不同也是很重要的。.就其他对象的实现而言,类继承定义了对象的实现。简言之,它是一种代码和命令共享的机制。相比之下,接口继承描述了对象可被用于替代另一个对象。

It's easy to confuse these two concepts,because many languages don't make the distinction explicit. In languages like C++and Eiffel, inheritance means both interface and implementation inheritance.The standard way to inherit an interface in C++ is to inherit publicly from a classthat has (pure) virtual member functions. Pure interface inheritance can beapproximated in C++ by inheriting publicly from

pure abstract classes. Pure implementation or class inheritance can be approximated with private inheritance. InSmalltalk, inheritance means just implementation inheritance. You can assign instances of any class to a variable as long as those instances support the operation performed on the value of the

variable.

这是两种难于理解的概念,因为很多语言没有说明两者的区别。在c++和eiffel中,继承意味着接口和实现的继承。在c++中继承接口的标准方法是继承来自类公共的成员函数:虚函数。在c++中,纯净接口继承可以通过继承公共的纯虚函数来实现。纯净的实现或类继承可以用私有继承近似实现。在Smalltalk中,继承仅仅意味着实现继承。你可以给变量分配任何实例化的类,只要这个实例化在变量的值范围内支持运算的实现。

Although most programming languages don'tsupport the distinction between interface and implementation inheritance,people make the distinction in practice. Smalltalk programmers usually act as if subclasses were subtypes (though there are some well-known exceptions [Coo92]);C++ programmers manipulate objects through types defined by abstract classes.

尽管很多语言不支持接口和实现继承的区分,在实践的过程中人们会对它做出区分。只要子类的类型只要不是众所周知的,smalltalk程序通常会做出行动。C++程序会通过抽象类定义的类型进行巧妙的控制。

Many of the design patterns depend on thisdistinction. For example, objects in a Chain of Responsibility (251) must have acommon type, but usually they don't share a common implementation. In theComposite (183) pattern, Component defines a common interface, but Composite oftendefines a common implementation. Command (263), Observer (326), State (338), andStrategy (349) are often implemented with abstract classes that are pure interfaces.

很多的设计模式会依赖这些区别。例如,在职责链模式中的对象必须有公共的类型,通常它们不会共享公共的实现。在组合模式中,组合定义了公共的接口,但是组合经常定义了公共的实现。命令模式,观察者模式,状态模式和策略模式用抽象类的纯接口来实现。