Java/Reflection/SuperClass
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Although the type of o2 is an interface, getSuperclass() returns the object"s superclass
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
}
}
Getting the Superclass of an Object
public class Main {
public static void main(String[] argv) throws Exception {
Object o = new String();
Class sup = o.getClass().getSuperclass(); // java.lang.Object
}
}
Retrieving other information through the class pointer
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());
}
}
Superclass of Object is null
public class Main {
public static void main(String[] argv) throws Exception {
Object o = new Object();
Class sup = o.getClass().getSuperclass(); // null
}
}
The superclass of primitive types is always null
public class Main {
public static void main(String[] argv) throws Exception {
Class cls = int.class;
Class sup = cls.getSuperclass(); // null
}
}