Java/Reflection/SuperClass
Содержание
Although the type of o2 is an interface, getSuperclass() returns the object"s superclass
<source lang="java">
public class Main {
public static void main(String[] argv) throws Exception { Runnable o2 = new Runnable() { public void run() { } }; Class sup = o2.getClass().getSuperclass(); // java.lang.Object }
}
</source>
Getting the Superclass of an Object
<source lang="java">
public class Main {
public static void main(String[] argv) throws Exception { Object o = new String(); Class sup = o.getClass().getSuperclass(); // java.lang.Object }
}
</source>
Retrieving other information through the class pointer
<source lang="java">
public class Main {
public static void main(String[] argv) throws Exception { Class cc = null; cc = Main.class; System.out.println("The name of the superclass is " + cc.getSuperclass()); System.out.println("Is SalariedEmployee an interface? " + cc.isInterface()); }
}
</source>
Superclass of Object is null
<source lang="java">
public class Main {
public static void main(String[] argv) throws Exception { Object o = new Object(); Class sup = o.getClass().getSuperclass(); // null }
}
</source>
The superclass of primitive types is always null
<source lang="java">
public class Main {
public static void main(String[] argv) throws Exception { Class cls = int.class; Class sup = cls.getSuperclass(); // null }
}
</source>