As in Java I want to know if my reference is declared as Interface.
function foo(classRef:Class){
if(classRef.isInterface(){
//something
}
}
Differences between a Class and an Interface:A class can be instantiated i.e, objects of a class can be created. An Interface cannot be instantiated i.e, objects cannot be created. Classes does not support multiple inheritance. Interface supports multiple inheritance.
When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. A class can implement multiple interfaces.
The getClass(). getInterfaces() method return an array of Class that represents the interfaces implemented by an object.
You can use AS3 Commons Reflect to get this information. Your function would then look something like this:
function foo(classRef:Class)
{
var type:Type = Type.forClass(classRef);
if (type.isInterface)
{
//something
}
}
My own exploring. If class is interface, than in description XML in <factory>
node it will never contain <constructor>
and <extendsClass>
. So, this is a function:
private function isInterface(type : *):Boolean {
var description : XML = describeType(type);
return (description.factory[0].descendants("constructor").length() == 0
&& description.factory[0].descendants("extendsClass").length() == 0);
}
Test:
trace(isInterface(IEventDispatcher));
trace(isInterface(Button));
trace(isInterface(int));
trace(isInterface(XML));
trace(isInterface(String));
Output:
[trace] true
[trace] false
[trace] false
[trace] false
[trace] false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With