Java/Reflection/SuperClass — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:01, 1 июня 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
  }
}