Java中的类 getSuperclass() 方法和示例
Java.lang.Class 类的getSuperclass()方法用于获取该实体的超类。这个实体可以是一个类、一个数组、一个接口等。方法返回这个实体的超类。
句法:
public Class getSuperclass()
参数:此方法不接受任何参数。
返回值:该方法返回该实体的超类。
下面的程序演示了 getSuperclass() 方法。
示例 1:
Java
// Java program to demonstrate getSuperclass() method
public class Test {
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for this class
Class myClass = Class.forName("Test");
System.out.println("Class represented by myClass: "
+ myClass.toString());
// Get the super class of myClass
// using getSuperclass() method
System.out.println("Superclass of myClass: "
+ myClass.getSuperclass());
}
}
Java
// Java program to demonstrate getSuperclass() method
public class Test {
class Arr {
}
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for Arr
Class arrClass = Arr.class;
// Get the super class of arrClass
// using getSuperclass() method
System.out.println("Superclass of arrClass: "
+ arrClass.getSuperclass());
}
}
输出:
Class represented by myClass: class Test
Superclass of myClass: class java.lang.Object
示例 2:
Java
// Java program to demonstrate getSuperclass() method
public class Test {
class Arr {
}
public static void main(String[] args)
throws ClassNotFoundException
{
// returns the Class object for Arr
Class arrClass = Arr.class;
// Get the super class of arrClass
// using getSuperclass() method
System.out.println("Superclass of arrClass: "
+ arrClass.getSuperclass());
}
}
输出:
Superclass of arrClass: class java.lang.Object
参考: https://docs.oracle.com/javase/9/docs/api/ Java/lang/Class.html#getSuperclass–