ArrayList collection1 = new ArrayList<Integer>();//通过,无warning ArrayList<Integer> collection2 = new ArrayList();//通过,有warning
ArrayList<String> collection3 = new ArrayList<Object>();//编译不通过 ArrayList<Object> collection4 = new ArrayList<String>();//编译不通过
ArrayList collection5 = new ArrayList<Integer>(); ArrayList<String> collection6 = collection5;//编译通过
ArrayList<? extends Number > collection1= new ArrayList<Integer >();//编译通过 ArrayList<? extends Number > collection2= new ArrayList<String>();//编译不通过
ArrayList<? super Integer > collection3= new ArrayList<Number >();//编译通过 ArrayList<? super Integer > collection4= new ArrayList<String>();//编译不通过
template <class T> T add(T x, T y){
return (T)(x+y);
}
public class GenericTest {
public static void main(String[] args) {
new GenericTest().testType();
}
public void testType(){
ArrayList<Integer> collection1 = new ArrayList<Integer>();
ArrayList<String> collection2= new ArrayList<String>();
System.out.println(collection1.getClass()==collection2.getClass());
//两者class类型一样,即字节码一致
System.out.println(collection2.getClass().getName());
//class均为java.util.ArrayList,并无实际类型参数信息
}
}
public class GenericTest {
public static void main(String[] args) {
swap(new String[]{"111","222"},0,1);//编译通过
//swap(new int[]{1,2},0,1);
//编译不通过,因为int不是引用类型
swap(new Integer[]{1,2},0,1);//编译通过
}
/*交换数组a 的第i个和第j个元素*/
public static <T> void swap(T[]a,int i,int j){
T temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
public class GenericTest {
public static void main(String[] args) {
new GenericTest().testType();
int a = biggerOne(3,5);
//int 和 double,取交为Number
Number b = biggerOne(3,5.5);
//String和int 取交为Object
Object c = biggerOne("1",2);
}
//从x,y中返回y
public static <T> T biggerOne(T x,T y){
return y;
}
}
swap(new String[3],1,2) -> static <E> void swap(E[]a,int i,int j)
add(3,5) -> static <T> T add(T a,T b)
fill(new Integer[3],3.5) -> static <T> void fill(T a[],T v)
int x = add(3,3.5) -> static <T> T add(T a,T b)
public class GenericDao<T>{
public void add(T x){
}
public T findById(int id){
return null;
}
public void delete(T obj){
}
public void delete(int id){
}
public void update(T obj){
}
public T findByUserName(String name){
return null;
}
public <T> Set<T> findByConditions(String where){
return null;
}
}
public class A<T>(){
//泛型类的成员方法,该T受A后面的T的限制
public T memberFunc(){
return null;
}
//泛型方法,这里的T和和类A的T是不同的
public static <T> T genericFunc(T a){
return null;
}
public static void main(String[] args) {
//编译不通过
//Integer i = A<String>().findByUserName("s");
//编译通过
Set<Integer> set= A<String>().findByConditions("s");
}
}
public class GenericTest {
public static void main(String[] args) throws Exception {
getParamType();
}
/*利用反射获取方法参数的实际参数类型*/
public static void getParamType() throws NoSuchMethodException{
Method method = GenericTest.class.getMethod("applyMap",Map.class);
//获取方法的泛型参数的类型
Type[] types = method.getGenericParameterTypes();
System.out.println(types[0]);
//参数化的类型
ParameterizedType pType = (ParameterizedType)types[0];
//原始类型
System.out.println(pType.getRawType());
//实际类型参数
System.out.println(pType.getActualTypeArguments()[0]);
System.out.println(pType.getActualTypeArguments()[1]);
}
/*供测试参数类型的方法*/
public static void applyMap(Map<Integer,String> map){
}
}
java.util.Map<java.lang.Integer, java.lang.String> interface java.util.Map class java.lang.Integer class java.lang.String
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有