使用接口在向量中传递两种不同类型的数据

时间:2022-09-25 16:09:55

In Actionscript3, as the title says, I'm trying to pass two different types of data in one vector through creating an interface for it. Does anybody have an example to do so?

在Actionscript3中,正如标题所说,我试图通过为它创建一个接口,在一个向量中传递两种不同类型的数据。有没有人有这样做的例子?

1 个解决方案

#1


2  

If they are unrelated objects, you might consider an Array instead. If you want the features of a Vector, then you just treat it like usual: create a Vector with the interface as the type instead of the most common base class.

如果它们是不相关的对象,则可以考虑使用数组。如果你想要Vector的功能,那么你就像平时那样对待它:创建一个以接口为类型而不是最常见的基类的Vector。

    var v:Vector.<IFace> = new Vector.<IFace>();

...and of course implement IFace on those classes you want to put into the Vector

......当然,在你想要放入Vector的类中实现IFace

UPDATE - OK, as an extension of the answer, here's an actual interface...

更新 - 好的,作为答案的扩展,这是一个实际的界面......

    public interface IFace
    {
        function doSomethingAllImplementersCanDo() : Boolean;
    }

...that would go in a file called IFace.as. A class that implements IFace might look like this...

...这将放在一个名为IFace.as的文件中。实现IFace的类可能看起来像这样......

    public class MonsterTruck implements IFace
    {
        public function doSomethingAllImplementersCanDo() : Boolean
        {
            return true;
        }
    }

...and somewhere else in your program...

......以及你程序中的其他地方......

        var v:Vector.<IFace> = new Vector.<IFace>();
        var mt:MonsterTruck = new MonsterTruck();
        v.push( mt );
        var throughIFace:IFace = v.pop();
        trace( throughIFace.doSomethingAllImplementersCanDo() );

I recommend checking out Adobe documentation here: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f41.html

我建议您在此处查看Adobe文档:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f41.html

#1


2  

If they are unrelated objects, you might consider an Array instead. If you want the features of a Vector, then you just treat it like usual: create a Vector with the interface as the type instead of the most common base class.

如果它们是不相关的对象,则可以考虑使用数组。如果你想要Vector的功能,那么你就像平时那样对待它:创建一个以接口为类型而不是最常见的基类的Vector。

    var v:Vector.<IFace> = new Vector.<IFace>();

...and of course implement IFace on those classes you want to put into the Vector

......当然,在你想要放入Vector的类中实现IFace

UPDATE - OK, as an extension of the answer, here's an actual interface...

更新 - 好的,作为答案的扩展,这是一个实际的界面......

    public interface IFace
    {
        function doSomethingAllImplementersCanDo() : Boolean;
    }

...that would go in a file called IFace.as. A class that implements IFace might look like this...

...这将放在一个名为IFace.as的文件中。实现IFace的类可能看起来像这样......

    public class MonsterTruck implements IFace
    {
        public function doSomethingAllImplementersCanDo() : Boolean
        {
            return true;
        }
    }

...and somewhere else in your program...

......以及你程序中的其他地方......

        var v:Vector.<IFace> = new Vector.<IFace>();
        var mt:MonsterTruck = new MonsterTruck();
        v.push( mt );
        var throughIFace:IFace = v.pop();
        trace( throughIFace.doSomethingAllImplementersCanDo() );

I recommend checking out Adobe documentation here: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f41.html

我建议您在此处查看Adobe文档:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f41.html