Java Reflection mechanism Demo (iv)-Gets the parent class of a class and implements the interface 1,java reflection mechanism to get the parent class of a class
Use the Getsuperclass () method in class to get the parent class of a class
If this Class
represents Object
a class, an interface, a base type, or void, NULL is returned. If this object represents an array class, the object representing the Object
class is returned Class
.
Test code:
Package Com.aaron.reflect;public class Demo4 {public static void main (string[] args) {class<?> C = integer.class; System.out.println ("The parent class of the integer is:" +c.getsuperclass ()); c = Number.class; System.out.println ("Number's parent class is:" +c.getsuperclass ()); c = Object.class; System.out.println ("The parent class of object is:" +c.getsuperclass ());}}
Operation Result:
The parent class of the integer is: Class Java.lang.NumberNumber's parent class is: Class Java.lang.ObjectObject's parent class is: null
Because Java does not have multiple inheritance, a class can have at most one parent class, so the return type is not an array.
2,java reflection mechanism returns an interface implemented by a class
When we develop an interface and implement it with a class, we see a class that knows what interface it implements, but when the machine itself gets a class, it does not know what interface it implements. Through the reflection mechanism, you can take the class object of an interface implemented by a class, so that you can explore the structure inside the interface.
A simple demo is given in this article.
First, the interface is defined as follows;
Package Com.aaron.reflect;public interface Animal {public static final String name = "Animal";p ublic String SayHello (strin g name);}
Then, define a class to implement this interface
Package Com.aaron.reflect;import Java.util.jar.attributes.name;public class Dog implements Animal {public final static String name = "Dog"; @Overridepublic string SayHello (String str) {return String.Format ("%s says:%s", this.name, str); }}
There is only one method in both the interface and the implementation class.
The code for the test class is as follows:
Package Com.aaron.reflect;public class Demo3 {public static void main (string[] args) {class<?> C = dog.class;//Get Dog The corresponding Class object class<?> interfaces[] = c.getinterfaces ();//Get all interfaces implemented by dog for (class<?> inte:interfaces) {// Print System.out.println ("Dog Implements Interface:" +inte);}}}
Operation Result:
Dog implementation interface: interface Com.aaron.reflect.Animal
Of course, Java does not have many inheritance, but it can implement multiple interfaces, so here an array is returned.
It is important to note that:
Class<?>[] Getinterfaces () in the array, the interface object Order and the order of the interface names in the Implements clause in the class represented by this object are consistent .
Java Reflection mechanism Demo (iv)-Gets the parent class of a class and the implemented interface