java.lang.Class.getSuperclass()方法实例
java.lang.Class.getSuperclass() 返回类表示此Class所表示的实体(类,接口,基本类型或void)的超类。
声明
以下是java.lang.Class.getSuperclass()方法的声明
public Class<? super T> getSuperclass()
参数
-
NA
返回值
此方法返回当前对象所表示的类的超类。
异常
-
NA
例子
下面的例子显示java.lang.Class.getSuperclass()方法的使用。
package com.yiibai; 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 = superClass super class of val1 = java.lang.Object val2 is object of type = subClass super class of val2 = superClass