Wednesday, December 8, 2010

How to make MultInheritance(Multiextends) in Action Script 3

If you recall yourself on the movie "Twins" with Arnold Schwarc. and Denny de Vito you probably understand what MultiInheritance mean. Childs from more then one parents. But In Action Script you could extends only one class (child from only one parent). This restrictions solves some problems like diamond problem when both parents come from same grandparent or awareness from which parent comes what, in child, when they have same function's or attribute's name. Still in some cases you would need MultInheritance(Multi extends).

public Class1
{
public var sameVarInAll:int=0;

public Class1(param1:*):void //whatever constructor arguments or no arguments
{
}

public function sameFunctionIn():void
{
}

public func1():void
{

}
}


public Class3
{
public var sameVarInAll:int=0;
public Class3(param3:*):void //whatever constructor arguments or noarguments
{
}

public function sameFunctionIn():void
{
}

public func3():void
{

}

}


public Class2
{

public var sameVarInAll:int=0;

public Class2(param2:*):void
{
}

public function sameFunctionIn():void
{
}

public func2():void
{

}

}


Let make interfaces from classes Class1,Class2,Class3.

interface IClass1
{
func1():void
}

interface IClass2
{
func2():void
}


interface IClass3
{
func3():void
}

Now we can extends interfaces IClass1,IClass2,IClass3

interface IMulti extends IClass1,IClass2,IClass3
{

}

and implements IMulti interface:

public class MultiInheretanceClass implements IMulti
{
private _class1:IClass1;
private _class2:IClass2;
private _class3:IClass3;


public class MultiInheretanceClass(param1:*,param2:*,param3:*):void
{
_class1=new Class1(param1);
_class2=new Class2(param2);
_class2=new Class3(param3);
}


public function set(value:int):void
{
//here we make decision from which class would be set
_class3.sameVarInAll=value;
}

public function get():int
{
//here we make decision from which class would be returned
return _class3.sameVarInAll;
}

public function sameFunctionIn():void
{
//here we make decision from which class would be called
_class.sameFunctionIn();//ex
}

func1():void
{
_class1.func1();
}

func2():void
{
_class2.func2();
}

func3():void
{
_class3.func3();
}

}

No comments:

Post a Comment