gpt4 book ai didi

actionscript-3 - Actionscript 中接口(interface)的主要功能是什么?

转载 作者:行者123 更新时间:2023-12-04 04:26:32 26 4
gpt4 key购买 nike

我知道包是类和接口(interface)的集合,旨在组织和分类其内容。我也知道类是对象的定义,以及对它们的说明,它们的属性/变量,以及它们的函数/方法。

但是,我还没有理解什么是接口(interface),或者它的真正用途...

我已经在 Adob​​e 的网站上阅读了这个定义..:

interface

Usage

interface InterfaceName [extends InterfaceName ] {}

定义一个接口(interface)。接口(interface)是定义一组方法的数据类型;这些方法必须由实现该接口(interface)的任何类定义。

接口(interface)类似于类,但有以下重要区别:

• Interfaces contain only declarations of methods, not their implementation. That is, every class that implements an interface must provide an implementation for each method declared in the interface.

Interface method definitions cannot have any attribute such as public or private, but implemented methods must be marked as public in the definition of the class that implements the interface.

• Multiple interfaces can be inherited by an interface by means of the extends statement, or by a class through the implements statement.

与 ActionScript 2.0 不同,ActionScript 3.0 允许在接口(interface)定义中使用 getter 和 setter 方法。

...但是,这太模糊了,对我没有帮助。

有人知道 ActionScript 中接口(interface)的用途和正确实现和/或设计吗?

最佳答案

接口(interface)基本上让你宣布“这个类可以做这些事情。”

举一个真实世界的例子,您可能想为一款游戏制作一个教程,在屏幕上逐一突出显示每个不同的控件。每个控件可能会闪烁或反弹以突出显示自身,因此您可以说它们实现了“IHighlightable”接口(interface),让它们处理其余部分:

public interface IHighlightable {

function highlight():void;

}

然后在你的控件中:

public class Control implements IHighlightable {

public function highlight():void {
// Bounce and flash!
}

}

这意味着您可以执行以下操作:

private function highlightControl(tutorialItem:IHighlightable):void {

tutorialItem.highlight();

}

重要的是,您可以让一个类实现多个接口(interface),这在类共享能力时很有用,但让它们都扩展一个公共(public)基类是没有意义的。

关于actionscript-3 - Actionscript 中接口(interface)的主要功能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15510210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com