Both interfaces andclasses can be forward declared. Forward declarations permit the creation of mutually dependent objects, for example:
接口和类可以都可以先被声明。(前置)声明允许相互依赖的对象被创建 例如:
<span style="font-size:14px;">Slice
module Family {
interface Child; // Forward declaration
sequence<Child*> Children; // OK
interface Parent {
Children getChildren(); // OK
};
interface Child { // Definition
Parent* getMother();
Parent* getFather();
};
};
</span>
Without the forward declaration of Child
, the definition obviously could not compile becauseChild
andParent
are mutually dependent interfaces. You can use forward-declared interfaces and classes to define types (such as theChildren
sequence in the previous example). Forward-declared interfaces and classes are also legal as the type of a structure, exception, or class member, as the value type of a dictionary, and as the parameter and return type of an operation. However, you cannot inherit from a forward-declared interface or class until after its definition has been seen by the compiler
如果没有先声明Child,那么这个定义显示不能编译, 因为Child和Parent是相互依赖的接口(Parent依赖Child),为了定义某个类型你可以提前声明某个类或接口(例如上个例子中的 Children sequence)。前置声明的接口/类是合法的,作为structure exeption 或 class member 类型,作为dictionary的值类型,作为方法的参数/返回值, 但是, 你不能继承一个前置声明的接口/类, 除非编译器已经有了它的定义
Slice
interface Base; // Forward declaration
interface Derived1 extends Base {}; // Error!
interface Base {}; // Definition
interface Derived2 extends Base {}; // OK, definition was seen
Not inheriting from a forward-declared base interface or class until its definition is seen is necessary because, otherwise, the compiler could not enforce that derived interfaces must not redefine operations that appear in base interfaces.
不能继承前置声明的类/接口知道它被定义是有必要的, 否则编译器不能确认子类/接口该如何重载基类/接口的方法。(这个和c++ 等语言一样)
注: A multi-pass compiler 是可以的,但增加的复杂性是不值得的。