在ActionScript 3的多重继承ActionScript

2023-09-08 13:16:30 作者:保持常温的感情是最好 别

在ActionScript 3的多重继承?可能吗?我读的地方,它有可能在AS3。

Multiple Inheritance in ActionScript 3? Is it possible? I have read somewhere that it is possible in as3.

如果是的话怎么办?

这是我的Doucument类A.as

this is my Doucument Class A.as

package
{
    import flash.display.MovieClip;

    public class A extends MovieClip implements B
    {    
        public var value1:Number=10;

        public function A()
        {
            trace("A Class Constructor");
        }
        public function hit():void
        {
            trace(value1+' from hit');   
        }
    }
}

另外一个是接口 B.as

    package
    {
       public interface B
       {
          trace(' interface ');
          function hit():void;
       }
    }

在此先感谢。

Thanks in advance.

推荐答案

多重继承是不可能为。但是,随着接口,你可以模仿一些多重继承的功能。 MI有重大缺陷,最明显的是钻石的问题: http://en.wikipedia.org/wiki/Diamond_problem 这就是为什么许多语言不支持心肌梗死,但只有单继承。 使用接口也出现你申请MI,但在现实中,是不是因为接口不提供实现,但功能只是一个承诺的情况。

Multiple inheritance is not possible in AS. But with interfaces you can mimic some of the functionality of multiple inheritance. MI has major flaws, most notably the diamond problem: http://en.wikipedia.org/wiki/Diamond_problem That's why many languages don't support MI, but only single inheritance. Using interfaces it "appears" you apply MI, but in reality that is not the case since interfaces don't provide an implementation, but only a promise of functionality.

interface BadAss{
    function doSomethingBadAss():void;
}

interface Preacher{
    function quoteBible():void;
}

class CrazyGangsta implements BadAss, Preacher{
    function quoteBible():void{
        trace( "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men." );
    }
    function doSomethingBadAss():void{
        //do something badass
    }
}

var julesWinnfield : CrazyGangsta = new CrazyGangsta();
julesWinnfield.doSomethingBadAss();
julesWinnfield.quoteBible();

//however, it mimics MI, since you can do:

var mofo : BadAss = julesWinnfield;
mofo.doSomethingBadAss();
//but not mofo.quoteBible();

var holyMan : Preacher = julesWinnfield;
holyMan.quoteBible();
//but not holyMan.doSomethingBadAss();

P.S:如果你想知道: 有没有钻石的问题,接口,因为接口的实现者必须提供在接口中定义的每个成员正好一个实现。所以,即使两个接口将定义相同的构件(当然,相同的签名)仍然会有仅一个实施

P.S.: In case you'd wonder: There's no diamond problem with interfaces since an implementor of the interfaces must provide exactly one implementation of each member defined in the interfaces. So, even if both interfaces would define the same member (with identical signature of course) there will still be only one implementation.