This page has been translated automatically.
编程
Fundamentals
Setting Up Development Environment
UnigineScript
High-Level Systems
C++
C#
UUSL (Unified UNIGINE Shader Language)
File Formats
Rebuilding the Engine and Tools
GUI
Double Precision Coordinates
应用程序接口
Containers
Common Functionality
Controls-Related Classes
Engine-Related Classes
Filesystem Functionality
GUI-Related Classes
Math Functionality
Node-Related Classes
Networking Functionality
Pathfinding-Related Classes
Physics-Related Classes
Plugins-Related Classes
Rendering-Related Classes
注意! 这个版本的文档是过时的,因为它描述了一个较老的SDK版本!请切换到最新SDK版本的文档。
注意! 这个版本的文档描述了一个不再受支持的旧SDK版本!请升级到最新的SDK版本。

接口类

使用接口对行为进行描述,这些行为被所有继承自接口的类所遵循。接口类并不会执行任何函数,这就意味着这种类仅包含对函数的声明。必须使用派生类才能执行接口类函数。

使用接口提供多形态。即数个类可对同一个接口函数通过不同的方法进行操作。

接口类

接口类的声明与UnigineScript中其它类的声明一样。 在此示例中,使用虚拟抽象函数声明

源代码 (UnigineScript)
class Interface {
	// 声明函数
	void update() = 0;
	//...;
}
注意Interface一字未被保留。

所有继承自Interface的类必须包括对函数的实施方法。例如:

源代码 (UnigineScript)
class Bar : Interface {
	// 执行用于Bar的接口函数
	void update() {
		log.message("Bar::update(): called\n");
	}
};
class Baz : Interface {
	// 执行用于Baz的接口函数
	void update() {
		log.message("Baz::update(): called\n");
	}
};

示例

试想存在一个对对象进行描述的接口类:

源代码 (UnigineScript)
class Interface {
	void update() = 0;
}
这就意味着每个对象必须被更新。

BarBaz 类描述了两种不同的对象。这两种类继承自Interface类且 Bar类派生自Foo类。

源代码 (UnigineScript)
class Foo {
	void foo() = 0;
};
class Bar : Foo, Interface {
	void update() {
		log.message("Bar::update(): called\n");
	}
};
class Baz : Interface {
	void update() {
		log.message("Baz::update(): called\n");
	}
};

使用此接口对不同类型的对象进行迭代,这些类型的对象执行对此接口的操作。因此可创建一个对象数组并将对象进行更新。

源代码 (UnigineScript)
Interface interfaces[0];
interfaces.append(new Bar());
interfaces.append(new Baz());
foreach(Interface i; interfaces) {
	i.update();
}
此示例输出以下内容:
输出
Bar::update(): called
Baz::update(): called

抽象虚拟函数声明

虚拟函数也是一种函数,可在派生类中将此函数覆盖。

注意
如果某个用户类继承自其它类,那么两种类都自动支持虚拟方法。

可通过下列方式(C++风格)声明虚拟函数:

源代码 (UnigineScript)
class Foo {
	void foo() = 0;
};
这种情况下,这样的函数并无任何实施方法而派生类必须对其进行实施。
注意
也可使用virtual关键字声明虚拟函数,但此为可选选项。
最新更新: 2017-07-03
Build: ()