Skip to content

Instantly share code, notes, and snippets.

@why168
Created December 26, 2018 12:59
Show Gist options
  • Save why168/6c9f7e4b2660315292f79744d55a0aaf to your computer and use it in GitHub Desktop.
Save why168/6c9f7e4b2660315292f79744d55a0aaf to your computer and use it in GitHub Desktop.
Java泛型
class Main {
public static void main(String[] args) {
Student.main();
}
static class Person<T> {
}
static class Student extends Person<Student> {
static void main() {
Student st = new Student();
Class clazz = st.getClass();
// getSuperclass()获得该类的父类
System.out.println(clazz.getSuperclass());
// getGenericSuperclass()获得带有泛型的父类
// Type是 Java 编程语言中所有类型的公共高级接口。它们包括原始类型、参数化类型、数组类型、类型变量和基本类型。
Type type = clazz.getGenericSuperclass();
System.out.println(type);
// ParameterizedType参数化类型,即泛型
ParameterizedType p = (ParameterizedType) type;
// getActualTypeArguments获取参数化类型的数组,泛型可能有多个
Class c = (Class) p.getActualTypeArguments()[0];
System.out.println(c);
// 输出内容
// class com.Main$Person
// com.Main.com.Main$Person<com.Main$Student>
// class com.Main$Student
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment