PDA

View Full Version : Confusing behavior using an interface


csdstudio
09-10-2006, 01:24 AM
This is driving me nuts, can someone explain why this is happening? The following code shows MainClass implementing Iextendable with a public var "owner". After instantiating that class you cannot set or retrieve the value of owner.

package
{
import flash.display.Sprite;
//
public class Doc extends Sprite
{
// PRIVATE PROPERTIES --------------------------------------------------------------------------------------------------
var ext1:Iextendable;
// CONSTRUCTOR ---------------------------------------------------------------------------------------------------------
public function Doc()
{
ext1=new MainClass();
trace(ext1.owner);// compiler error, not found
trace(ext1.ownerID);
}
}
//
interface Iextendable
{
function get ownerID():String;
}
//
class MainClass implements Iextendable
{
public var owner:String="owner string";
private var _ownerID:Number;
public function extendable(){

}
public function get ownerID():Number
{
return _ownerID;
}
}
}


Now if I change the data type of ext1 to MainClass it works fine:


package
{
import flash.display.Sprite;
//
public class Doc extends Sprite
{
// PRIVATE PROPERTIES --------------------------------------------------------------------------------------------------
var ext1:MainClass; //changed type from 'Iextendable'
// CONSTRUCTOR ---------------------------------------------------------------------------------------------------------
public function Doc()
{
ext1=new MainClass();
trace(ext1.owner);// now traces "owner string"
trace(ext1.ownerID);
}
}
//
interface Iextendable
{
function get ownerID():String;
}
//
class MainClass implements Iextendable
{
public var owner:String="owner string";
private var _ownerID:Number;
public function extendable(){

}
public function get ownerID():Number
{
return _ownerID;
}



What this tells me is any variable not declared in the interface getter/setter requirements, cannot be accessed. That doesn't make sense.

Tink
09-10-2006, 11:06 AM
It makes perfect sense.

Iextendable doesn't have the prop 'owner'.

MainClass does have the prop 'owner'.e.

you have 'ext1' typed as an Iextendable, and therefore it can't be found.

csdstudio
09-10-2006, 09:58 PM
Then that is only true for interfaces, take the following code for example. By typing ext1 to MainClass, owner2 isn't defined in MainClass, but nothing errors out like the interface example.


package
{
import flash.display.Sprite;
//
public class Doc extends Sprite
{
// PRIVATE PROPERTIES --------------------------------------------------------------------------------------------------
var ext1:MainClass;
// CONSTRUCTOR ---------------------------------------------------------------------------------------------------------
public function Doc()
{
ext1=new SubClass();
trace(ext1.owner);//traces MainClass
trace(ext1.owner2);//traces SubClass
}
}
//
class MainClass
{
public var owner:String="MainClass";
}
//
class SubClass extends MainClass
{
public var owner2:String="SubClass";
}
}