OOP help

Hi,

Im trying out some kind of decorator design pattern style OOP and im having some problems.

base class:

class cBaseClass
{
	var sName:String = "base name";
	
	function desc()
	{
		return "base class desc";
	}
}

Extended class:

class cExtendedClass extends cBaseClass
{
	var base:cBaseClass;
	
	function cExtendedClass(p:cBaseClass)
	{
		base = p;
	}
	
	function desc()
	{
		return base.desc()+" AND extended class desc";
	}
}

test script:

var mytest:cBaseClass = new cBaseClass();
mytest = new cExtendedClass(mytest);
Debug.Log("desc = "+mytest.desc());

mytest is showing as an cExtendedClass object but its still acting like a cBaseClass object. It wont override the cBaseClass desc() function or recognise any other functions if you add them to cExtendedClass etc.

I found if you use:
Debug.Log("desc = "+(mytest as cExtendedClass).desc());
it works as it should.

Can anyone figure out what im doing wrong here please?

I am no pro with casting in Javascript but you have an implicit cast in your code.

mytest = new cExtendedClass(mytest);

this line reads after parsing:

mytest = new cExtendedClass(mytest) as cBaseClass;

because you explicitly tell the parser that your var mytest is a cBaseClass.

For correct behavior you could use a second var typed as cExtendedClass or dont explicitly type mytest.

Thanks for the reply vastor but that didnt work.

After digging around a few hours, I found if you make the desc function in those classes as ‘virtual’, the behaviour works as intended.

virtual function desc() {
.
.
.
}