Multiple Inheritance in ActionScript 3

前端 未结 4 1120
半阙折子戏
半阙折子戏 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.

    0 讨论(0)
  • 2021-02-06 06:34

    Umm, guys!

    I can show you two different ways to "mimic" multiple inheritance:

    1. Using the #import feature: http://archive.darronschall.com/weblog/2006/10/multiple-inheritance-in-actionscript-3.html

    2. Don't forget that ActionScript is EcmaScript. It's still a prototype based language: http://jolierouge.net/2010/01/as3-multiple-inheritancemixins/

    I know... not "real" MI, but they look like a duck, speak like a duck...

    0 讨论(0)
  • 2021-02-06 06:44

    Well there is no possibility of multiple inheritance directly in AS3 like many of other OOP languages.

    OOP is about reusing code and like most of us want to reuse the code written in multiple classes. So if you really mean to reuse the code (logic) instead of just the signatures you might want to consider composition or deligation approaches and probably this is what you have read somewhere, like you said.

    In composition what you do is instead of inheriting a baseclass into subclass, you will have an instance of the baseclass in a subclass and have all the methods

    package
    {
        public class BaseClass1
        {
            public function someMethod( )
            {
                trace("This is from the BaseClass1");
            }
        }
    }
    
    package
    {
        public class BaseClass2
        {
            public function anotherMethod( )
            {
                trace("This is from the BaseClass2");
            }
        }
    }
    package
    {
        //Composition
        public class HasBase
        {
            private var baseClass1:BaseClass1;
            private var baseClass2:BaseClass2;
            public function HasBase( )
            {
                baseClass1=new BaseClass1( );
                baseClass2=new BaseClass2( );
            }
            public function someMethod( )
            {
                baseClass1.someMethod( );
            }
            public function anotherMethod(){
                baseClass2.anotherMethod();
            }
        }
    }
    

    This is not a hack but actually a real and practical implementation adopted by experienced developers in many design patterns.

    Hope this helps

    0 讨论(0)
  • 2021-02-06 06:51

    You can do the multiple inheritance using the interface.

    1. Define Class A,B and C. and Define interface for the Class B and C respectively iB and iC.
    2. Just extends the Class C to Class A using "extend" keyword - Direct Inheritance of class
    3. extends your interface iC to the interface iB.
    Just check code as per your requirement:
    
    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');
          }
          public function print():void
          {
              trace("Print Method Called");
          }
      }
    } 
    
    package
    {
      public interface B extends D 
      {
          function hit():void;
      }
    }
    
    package
    {
      public class C implements D
      {
          pulic function C()
          {
              trace("C Class Constructor");
          }
          public function print():void
          {
              trace("Print Method Called");
          }
      }
    }
    
    package
    {
      public interface D
      {
          function print():void;
      }
    }
    
    0 讨论(0)
提交回复
热议问题