简体   繁体   English

在Actionscript-3和其他语言中存在接口的原因是什么

[英]What's the reason for interface to exist in Actionscript-3 and other languages

what is the meaning of this interfaces? 这个接口是什么意思? even if we implement an interface on a class, we have to declare it's functionality again and again each time we implement it on a different class, so what is the reason of interfaces exist on as3 or any other languages which has interface. 即使我们在类上实现了一个接口,我们每次在不同的类上实现它时都要反复声明它的功能,所以as3或任何其他具有接口的语言都存在接口的原因是什么。 Thank you 谢谢

I basically agree with the answers posted so far, just had a bit to add. 我基本同意到目前为止发布的答案,只是有点补充。

First to answer the easy part, yes other languages have interfaces. 首先回答简单的部分,是的其他语言有接口。 Java comes to mind immediately but I'm pretty sure all OOP languages (C++, C#, etc.) include some mechanism for creating interfaces. Java立即浮现在脑海中,但我很确定所有OOP语言(C ++,C#等)都包含一些创建接口的机制。

As stated by Jake, you can write interfaces as "contracts" for what will be fulfilled in order to separate work. 正如Jake所说,您可以将接口编写为“合同”,以便分离工作。 To take a hypothetical say I'm working on A and you're working on C, and bob is working on B. If we define B' as an interface for B, we can quickly and relatively easily define B' (relative to defining B, the implementation), and all go on our way. 假设我正在研究A并且你正在研究C,并且bob正在研究B.如果我们将B'定义为B的接口,我们可以快速且相对容易地定义B'(相对于定义B,实施),一切都在继续。 I can assume that from AI can code to B', you can assume from C you can code to B', and when bob gets done with B we can just plug it in. 我可以假设从AI可以编码到B',你可以假设从C开始你可以编码到B',当bob完成B时我们可以插入它。

This comes to Jugg1es point. 这是Jugg1es的观点。 The ability to swap out a whole functional piece is made easier by "dependency injection" (if you don't know this phrase, please google it). 通过“依赖注入”可以更换掉整个功能块的能力(如果你不知道这个短语,请google它)。 This is the exact thing described, you create an interface that defines generally what something will do, say a database connector. 这就是所描述的确切内容,您创建了一个接口,通常定义了某些内容,例如数据库连接器。 For all database connectors, you want it to be able to connect to database, and run queries, so you might define an interface that says the classes must have a "connect()" method and a "doQuery(stringQuery)." 对于所有数据库连接器,您希望它能够连接到数据库并运行查询,因此您可以定义一个接口,该接口说明类必须具有“connect()”方法和“doQuery(stringQuery)”。 Now lets say Bob writes the implementation for MySQL databases, now your client says well we just paid 200,000 for new servers and they'll run Microsoft SQL so to take advantage of that with your software all you'd need to do is swap out the database connector. 现在让我们说Bob编写了MySQL数据库的实现,现在你的客户端说我们刚刚为新服务器支付了200,000,他们将运行Microsoft SQL,以便利用你的软件利用你需要做的就是换掉数据连接器。

In real life, I have a friend who runs a meat packing/distribution company in Chicago. 在现实生活中,我有一位朋友在芝加哥经营一家肉类包装/分销公司。 The company that makes their software/hardware setup for scanning packages and weighing things as they come in and out (inventory) is telling them they have to upgrade to a newer OS/Server and newer hardware to keep with the software. 制造软件/硬件设置以进行扫描包装和称重物品进入和退出(库存)的公司告诉他们,他们必须升级到更新的操作系统/服务器和更新的硬件才能与软件保持一致。 The software is not written in a modular way that allows them to maintain backwards compatibility. 该软件不是以模块化方式编写的,允许它们保持向后兼容性。 I've been in this boat before plenty of times, telling someone xyz needs to be upgraded to get abc functionality that will make doing my job 90% easier. 我曾多次在这艘船上,告诉某人xyz需要升级才能获得abc功能,这将使我的工作变得更容易90%。 Anyhow guess point being in the real world people don't always make use of these things and it can bite you in the ass. 无论如何猜测在现实世界中人们并不总是利用这些东西,它可以咬你的屁股。

Interfaces are vital to OOP, particularly when developing large applications. 接口对OOP至关重要,特别是在开发大型应用程序时。 One example is if you needed a data layer that returns data on, say, Users. 例如,如果您需要一个数据层来返回数据,例如,用户。 What if you eventually change how the data is obtained, say you started with XML web services data, but then switched to a flat file or something. 如果你最终改变了数据的获取方式怎么办,比如你开始使用XML Web服务数据,然后切换到平面文件或其他东西。 If you created an interface for your data layer, you could create another class that implements it and make all the changes to the data layer without ever having to change the code in your application layer. 如果为数据层创建了一个接口,则可以创建另一个实现它的类,并对数据层进行所有更改,而无需更改应用程序层中的代码。 I don't know if you're using Flex or Flash, but when using Flex, interfaces are very useful. 我不知道你是使用Flex还是Flash,但是在使用Flex时,接口非常有用。

Interfaces are a way of defining functionality of a class. 接口是一种定义类功能的方法。 it might not make a whole lot of sense when you are working alone (especially starting out), but when you start working in a team it helps people understand how your code works and how to use the classes you wrote (while keeping your code encapsulated). 当你独自工作(特别是刚开始)时,它可能没有多大意义,但当你开始在团队中工作时,它可以帮助人们理解你的代码如何工作以及如何使用你编写的类(同时保持代码封装) )。 That's the best way to think of them at an intermediate level in my opinion. 在我看来,这是在中间水平考虑它们的最佳方式。

While the existing answers are pretty good, I think they miss the chief advantage of using Interfaces in ActionScript, which is that you can avoid compiling the implementation of that Interface into the Main Document Class. 虽然现有的答案非常好,但我认为他们错过了在ActionScript中使用Interfaces的主要优势,即您可以避免将该接口的实现编译到Main Document Class中。

For example, if you have an ISpaceShip Interface, you now have a choice to do several things to populate a variable typed to that Interface. 例如,如果您有一个ISpaceShip接口,您现在可以选择执行几项操作来填充键入该接口的变量。 You could load an external swf whose main Document Class implements ISpaceShip. 您可以加载主Document类实现ISpaceShip的外部swf。 Once the Loader's contentLoaderInfo 's COMPLETE event fires, you cast the content to ISpaceShip, and the implementation of that (whatever it is) is never compiled into your loading swf. 一旦Loader的contentLoaderInfo的COMPLETE事件触发,你将content为ISpaceShip,并且它的实现(无论它是什么)永远不会被编译到你的加载swf中。 This allows you to put real content in front of your users while the load process happens. 这允许您在加载过程发生时将实际内容放在用户面前。

By the same token, you could have a timeline instance declared in the parent AS Class of type ISpaceShip with "Export for Actionscript in Frame N *un*checked. This will compile on the frame where it is first used, so you no longer need to account for this in your preloading time. Do this with enough things and suddenly you don't even need a preloader. 出于同样的原因,你可以在ISpaceShip类型的父AS类中声明一个时间轴实例,其中“在框架N *中检查导出动作脚本”。这将在首次使用它的框架上编译,因此您不再需要在预加载时间考虑到这一点。用足够的东西做这件事突然你甚至不需要预装载器。

Another advantage of coding to Interfaces is if you're doing unit tests on your code, which you should unless your code is completely trivial. 编写接口的另一个优点是,如果您正在对代码进行单元测试,除非您的代码完全无关紧要。 This enables you to make sure that the code is succeeding or failing on its own merits, not based on the merits of the collaborator, or where the collaborator isn't appropriate for a test. 这使您可以确保代码在其自身优点上成功或失败,而不是基于协作者的优点,或者协作者不适合测试。 For example, if you have a controller that is designed to control a specific type of View, you're not going to want to instantiate the full view for the test, but only the functionality that makes a difference for the test. 例如,如果您有一个旨在控制特定类型View的控制器,那么您不会想要实例化测试的完整视图,而只想实现对测试产生影响的功能。

If you don't have support in your work situation for writing tests, coding to interfaces helps make sure that your code will be testable once you get to the point where you can write tests. 如果您在编写测试的工作情况中没有支持,那么编写接口有助于确保在您可以编写测试之后,代码将是可测试的。

The above answers are all very good, the only thing I'd add - and it might not be immediately clear in a language like AS3, where there are several untyped collection classes (Array, Object and Dictionary) and Object/dynamic classes - is that it's a means of grouping otherwise disparate objects by type. 上面的答案都非常好,我唯一要添加的东西 - 它可能不会立即用AS3之类的语言清楚地表达,其中有几个无类型的集合类(数组,对象和字典)和对象/动态类 - 是它是一种按类型分组其他不同对象的方法。

A quick example: 一个简单的例子:

Image you had a space shooter, where the player has missiles which lock-on to various targets. 你有一个太空射击者的图像,玩家拥有可以锁定各种目标的导弹。 Suppose, for this purpose, you wanted any type of object which could be locked onto to have internal functions for registering this (aka an interface): 假设,为此目的,您希望任何类型的对象可以被锁定以具有用于注册它的内部函数(也称为接口):

function lockOn():void;//Tells the object something's locked onto it

function getLockData():Object;//Returns information, position, heat, whatever etc

These targets could be anything, a series of totally unrelated classes - enemy, friend, powerup, health. 这些目标可以是任何东西,一系列完全不相关的类 - 敌人,朋友,通电,健康。

One solution would be to have them all to inherit from a base class which contained these methods - but Enemies and Health Pickups wouldn't logically share a common ancestor (and if you find yourself making bizarre inheritance chains to accomodate your needs then you should rethink your design!), and your missile will also need a reference to the object its locked onto: 一种解决方案是让它们全部从包含这些方法的基类继承 - 但是敌人和健康拾取器在逻辑上不会共享共同的祖先(如果你发现自己制造奇怪的继承链以满足你的需求那么你应该重新思考你的设计!),你的导弹还需要引用它锁定的物体:

var myTarget:Enemy;//This isn't going to work for the Powerup class!

or 要么

var myTarget:Powerup;//This isn't going to work for the Enemy class!

...but if all lockable classes implement the ILockable interface, you can set this as the type reference: ...但是如果所有可锁定类都实现了ILockable接口,则可以将其设置为类型引用:

var myTarget:ILockable;//This can be set as Enemy, Powerup, any class which implements ILockable!

..and have the functions above as the interface itself. ..并将上述函数作为接口本身。




They're also handy when using the Vector class (the name may mislead you, it's just a typed array) - they run much faster than arrays, but only allow a single type of element - and again, an interface can be specified as type: 它们在使用Vector类时也很方便(名称可能误导你,它只是一个类型化的数组) - 它们比数组运行得快得多,但只允许单一类型的元素 - 同样,接口可以指定为类型:

var lockTargets:Vector.<Enemy> = new Vector.<Enemy>();//New array of lockable objects

lockTargets[0] = new HealthPickup();//Compiler won't like this!

but this... 但是这个...

var lockTargets:Vector.<ILockable> = new Vector.<ILockable>();

lockTargets[0] = new HealthPickup();
lockTargets[1] = new Enemy();

Will, provided Enemy and HealthPickup implement ILockable , work just fine! Will,如果EnemyHealthPickup实现了ILockable ,工作得很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM