开发者

Finding out what type a certain interface is

I am creating a method that accepts a IOBJECT parameter. there is multiple class that implement this inte开发者_如何学Pythonrface. I need to figure out which type IOBJECT is. how would i go about doing that


You can use typeof, instanceof, or the 'is' operator


It's not ideal, but you can use the "is" operator. Throw it into a switch of if else statment to figure things out.

if(obj is ClassA) {
    //sweetness
} else if (obj is ClassB) {
    //awesomeness
}

typeof will not work, as suggested in the other reply. It will likely return "object" in all cases. instanceof will work though.


You can do getQualifiedClassName() to get the class name of the object. You can also use describeType() which gives you a more complete description of all the methods and properties of the object.

There is information about both here: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html

It doesn't sound like an ideal situation though. You may want to do something where you can standardize the way you handle all items. For example:

public interface IObject { 
    function doSomething():void;
}

Then...

function myMethod(obj:IObject):void {
    obj.doSomething();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜