public interface TypeVariable<D extends GenericDeclaration> extends Type {
//获得泛型的上限,若未明确声明上边界则默认为Object
Type[] getBounds();
//获取声明该类型变量实体(即获得类、方法或构造器名)
D getGenericDeclaration();
//获得名称,即K、V、E之类名称
String getName();
}
//1.在类(Class)上声明(定义)类型变量
class A<T>{
T a;
}//之后这里可用任意类型替换T,例如
A<String> as = new A<String>();
//是否看着有点像集合?不错,集合就是泛型的一个典型运用
//2.在方法上声明(定义)
public <E> void test(E e){}
//方法上,类型变量声明(定义)不是在参数里边,而且必须在返回值之前,static等修饰后
//3.声明(定义)在构造器上
public <K> A(K k){}
//假设
class A<T super classA>{
T t;
public void test(){
//这个时候你不能用t干任何事,因为你不确定t具有哪些属性和方法
//当然,t肯定是有Object方法的,但没意义
}
}
class A<K extends classA & interfaceB, V>{
K key;
V value;
public static void main(String[] args) throws Exception
{
Type[] types = Main.class.getTypeParameters();
for(Type type : types){
TypeVariable t = (TypeVariable)type;
System.out.println(t.getGenericDeclaration());
int size = t.getBounds().length;
System.out.println(t.getBounds()[size - 1]);
System.out.println(t.getName() + "\n-------------分割线-------------");
}
}
}
//输出结果
class com.fcc.test.Main
interface com.fcc.test.interfaceB
K
-------------分割线-------------
class com.fcc.test.Main
class java.lang.Object
V
-------------分割线-------------
Constructor constructor = Main.class.getConstructor(); TypeVariable typeVariable = constructor.getTypeParameters()[0]; System.out.println(typeVariable.getGenericDeclaration()); //获得方法中声明(定义)的类型变量与上面类似
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有