ActionScript 3 中的多重继承 [英] Multiple Inheritance in ActionScript 3

查看:26
本文介绍了ActionScript 3 中的多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ActionScript 3 中的多重继承?是否可以?我在某处读到在 as3 中是可能的.

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

如果是,那么如何?

这是我的 A 类文档

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;
       }
    }

提前致谢.

推荐答案

多重继承在 AS 中是不可能的.但是通过接口,您可以模仿多重继承的一些功能.MI 有重大缺陷,最明显的是钻石问题:http://en.wikipedia.org/wiki/Diamond_problem这就是为什么许多语言不支持 MI,而只支持单继承.使用它出现"的接口,您可以应用 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.

这篇关于ActionScript 3 中的多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆