Multiple Inheritance in ActionScript 3

前端 未结 4 1123
半阙折子戏
半阙折子戏 2021-02-06 05:50

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

If yes then how?

this is my Doucument Class A.as

<
4条回答
  •  余生分开走
    2021-02-06 06:34

    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.: 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.

提交回复
热议问题