Jump to content
  • Advertisement
Sign in to follow this  
BloodyEpi

[as3] inheritance

This topic is 2526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey guys,

i have a problem with inheritance. i have a base class and two derived classes, now theres one member i need in both derived classes, but the member in each class has a different type.

at the moment it looks like this:
baseclass

public class Base
{
private var m:Object;

protected function setM(m:Object):void
{
this.m = m;
}

protected function getM():Object
{
return this.m;
}

public function getMAsBool():Boolean
{
throw new Error("Wrong Type");
}

public function getMAsNumber():Number
{
throw new Error("Wrong Type");
}

public function setMAsBool(signal:Boolean):void
{
throw new Error("Wrong Type");
}

public function setMAsNumber(signal:Number):void
{
throw new Error("Wrong Type");
}
}

derived1:

public class Derived1 extends Base
{
override public function setMAsBool(signal:Boolean):void
{
setM(signal);
}

override public function getMAsBool():Boolean
{
return getM();
}
}


at the end i want to create a vector with these derived classes and call the getter and setter functions typesafe

var v:Vector.<Base>;
v[0].setAsBool(true); //works if of type Derived1, otherwise exception


this works as i want it to, but the base class will get larger with every new membertype and it looks ugly:D so im pretty sure i have either a problem with my design or there must be some best practice or something else, because i dont think im the first with this problem, or maybe there is a solution especially for as3? i couldnt find anything

language is as3, so theres no templates(besides vector<>) and function overloading

Share this post


Link to post
Share on other sites
Advertisement
well, i have different modules, and they have different input and output signals and signaltypes.

lets say i have module1 with one bool and one float input, now i want to add these inputsignals to the module.
later on i can do something like "module1.input[0].set(true)" to set the first inputsignal to the boolean value true or "module1.input[1].get()"to get the number of the second inputsignal.

is there an easier way to do this?

Share this post


Link to post
Share on other sites
So what exactly makes your input classes any different than just a normal variable?

In which case you would see that you are ending up with an Array of untyped variables which should scream 'bad design' :P

Maybe if you could give more information we might be able to suggest alternative designs

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!