Java Class getSuperclass()方法

java.lang.Class.getSuperclass() 返回类表示此Class所表示的实体(类,接口,基本类型或void)的超类。

1 语法

public Class<? super T> getSuperclass()

2 参数

3 返回值

此方法返回当前对象所表示的类的超类。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Class getSuperclass()方法
 */
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

 

热门文章

优秀文章