Java类类getGenericSuperclass()方法及示例

类类getGenericSuperclass()方法 (Class class getGenericSuperclass() method)

  • getGenericSuperclass() method is available in java.lang package.

    getGenericSuperclass()方法在java.lang包中可用。

  • getGenericSuperclass() method is used to return the Type denoting the generic superclass of the class or an interface or primitive type or void denoted by this Class directly.

    getGenericSuperclass()方法用于返回表示类的泛型超类的Type或直接由该Class表示的接口或原始类型或void。

  • getGenericSuperclass() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getGenericSuperclass()方法是一个非静态方法,只能通过类对象进行访问,如果尝试使用类名访问该方法,则会收到错误消息。

  • getGenericSuperclass() method may throw an exception at the time of returning a generic superclass.

    返回通用超类时, getGenericSuperclass()方法可能会引发异常。

    • GenericSignatureFormatError: This exception may raise when the generic class signature does not match the format given in the JVM Specification.GenericSignatureFormatError :当通用类签名与JVM规范中给定的格式不匹配时,可能会引发此异常。
    • TypeNotPresentException: This exception may raise when any generic superclass refers to a non-existent type.TypeNotPresentException :当任何泛型超类引用不存在的类型时,可能会引发此异常。
    • MalformedParameterizedTypeException: This exception may raise when any generic superclass refers to a parameterized type that cannot be initialized at any cost.MalformedParameterizedTypeException :当任何泛型超类引用无法以任何代价初始化的参数化类型时,可能会引发此异常。

Syntax:

句法:

    public Type getGenericSuperclass();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is Type, it returns the super class of the entity denoted by this object.

此方法的返回类型为Type ,它返回此对象表示的实体的超类。

Example:

例:

// Java program to demonstrate the example 
// of Type  getGenericSuperclass () method of Class 

import java.lang.reflect.*;
import java.util.*;

public class GetGenericSuperClassOfClass {
 public static void main(String[] args) {
  // It returns the generic super class of
  // the class GenericClass

  Type ty = GenericClass.class.getGenericSuperclass();

  if (ty != null) {
   System.out.print("Generic Super class of GenericClass: ");
   System.out.println(ty);
  } else
   System.out.println("No super class exists");
 }
}

class GenericClass extends HashSet {
 public GenericClass() {}
}

Output

输出量

Generic Super class of GenericClass: class java.util.HashSet


翻译自: https://www.includehelp.com/java/class-class-getgenericsuperclass-method-with-example.aspx