Java.lang.Class.getSuperclass() 方法
描述
java.lang.Class.getSuperclass() 返回表示该类所表示的实体(类、接口、原始类型或 void)的超类的类。
声明
以下是 java.lang.Class.getSuperclass() 方法的声明。
public Class<? super T> getSuperclass()
参数
NA
返回值
该方法返回该对象所代表的类的超类。
异常
NA
示例
下面的例子展示了 java.lang.Class.getSuperclass() 方法的使用。
package com.tutorialspoint; import java.lang.*; class superClass { // super class } class subClass extends superClass { // sub class } public class ClassDemo { public static void main(String args[]) { superClass val1 = new superClass(); subClass val2 = new subClass(); Class cls; cls = val1.getClass(); System.out.println("val1 is object of type = " + cls.getName()); /* returns the superclass of the class(superClass) represented by this object */ cls = cls.getSuperclass(); System.out.println("super class of val1 = " + cls.getName()); cls = val2.getClass(); System.out.println("val2 is object of type = " + cls.getName()); /* returns the superclass of the class(subClass) represented by this object */ cls = cls.getSuperclass(); System.out.println("super class of val2 = " + cls.getName()); } }
让我们编译并运行上面的程序,这将产生下面的结果 −
val1 is object of type = com.tutorialspoint.superClass super class of val1 = java.lang.Object val2 is object of type = com.tutorialspoint.subClass super class of val2 = com.tutorialspoint.superClass