Multiple Inheritance in ActionScript 3

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

提交回复
热议问题