Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify if Class reference is an interface?

As in Java I want to know if my reference is declared as Interface.

function foo(classRef:Class){

if(classRef.isInterface(){
  //something
}

}
like image 654
Mady Avatar asked Oct 10 '11 10:10

Mady


People also ask

How do you identify if a class is an interface?

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.

Can reference type BE interface?

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.

What makes a class an 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.

How do you find the class interface?

The getClass(). getInterfaces() method return an array of Class that represents the interfaces implemented by an object.


2 Answers

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
    }
}
like image 90
Gerhard Schlager Avatar answered Oct 07 '22 23:10

Gerhard Schlager


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
like image 43
Timofei Davydik Avatar answered Oct 08 '22 00:10

Timofei Davydik