黑马程序员--加强之泛型
来源:互联网 发布:金十数据_官方网站 编辑:程序博客网 时间:2024/05/02 08:47
-------android培训、java培训、期待与您交流! ----------
package cn.itcast.day2;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.Hashtable;import java.util.Map;import java.util.Set;import java.util.Vector;import cn.itcast.day1.ReflectPoint;public class GenericTest{/** * @param args * @throws NoSuchMethodException * @throws SecurityException * @throws InvocationTargetException * @throws IllegalAccessException * @throws InstantiationException * @throws IllegalArgumentException * @throws ClassNotFoundException */public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException{ArrayList collection1 = new ArrayList();collection1.add(1);collection1.add(2L);collection1.add("abc");int i = (Integer) collection1.get(0);//int i = (int) collection1.get(0);ArrayList<String> collection2 = new ArrayList<String>();//collection2.add(1); // is not applicable for the argument//collection2.add(2L);collection2.add("abc");String s = collection2.get(0);// new String(new StringBuffer("abc");//带泛型的反射Constructor<String> constructor1 = String.class.getConstructor(StringBuffer.class);String str1 = constructor1.newInstance(new StringBuffer("abc"));System.out.println(str1.charAt(2));//Generic type retent at the source only.ArrayList<Integer> collection3 = new ArrayList<Integer>();System.out.println(collection3.getClass() == collection2.getClass());//true//collection3.add("abc") by reflectMethod methodAdd = collection3.getClass().getMethod("add", Object.class);boolean retVal = (Boolean)methodAdd.invoke(collection3, "abc");System.out.println(retVal);Object str2 = collection3.get(0);System.out.println(str2);printCollection(collection3);printCollection2(collection3);//Class<? extends Number> x = Object.class.asSubclass(Number.class);//compile ok, but run errClass<? extends Number> x = Number.class.asSubclass(Number.class);//run okClass<?> y;//Class<String> x1 = Class.forName("java.lang.String");//Compile errClass<?> x1 = Class.forName("java.lang.String");//Compile OKClass<String> x2 = (Class<String>) Class.forName("java.lang.String");//Compile OKHashMap<String,Integer> maps = new HashMap<String,Integer>();maps.put("a",28);maps.put("b",21);maps.put("c",38);Set<String> set = maps.keySet();for(String key : set){System.out.println(key+":"+maps.get(key));}Set<Map.Entry<String, Integer>> entrySet = maps.entrySet();for(Map.Entry<String, Integer> entry: entrySet){System.out.println(entry.getKey()+":"+entry.getValue());}//泛型应用add(3,5);Number x11 = add(3.5, 3); //在Number 范围内 double 与 int可相加Object x22 = add(3, "abc"); //在Object范围内int 与 String 相加swap(new String[]{"abc", "xyz", "whb"}, 1, 2);swap(new Integer[]{100, 300, 200}, 1, 2);Object obj = "abc";String str3 = autoConvertType(obj);//泛型推演copy1(new Vector<String>(), new String[10]);copy2(new Date[10], new String[10]);//!define one generic type is enough.//copy1(new Vector<Date>(), new String[10]);// err. not applicable for the argument//泛型在DAO中的应用GnericDAO<ReflectPoint> dao = new GnericDAO<ReflectPoint>();dao.add(new ReflectPoint(3, 5));//String e = dao.findById(1); //type err can be prompted when compillingReflectPoint e = dao.findById(1);//Vector<Date> v1 = new Vector<Date>();Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class);Type[] types = applyMethod.getGenericParameterTypes();//得到方法上的类型定义,其中包含泛型for(Type type : types){if(type instanceof ParameterizedType){ParameterizedType pType = (ParameterizedType)type;System.out.println(pType.getRawType());//this type is in the class fileSystem.out.println(pType.getOwnerType());System.out.println(Arrays.toString(pType.getActualTypeArguments()));}System.out.println();System.out.println(type.getClass());}//HashMap<String,Date> h1 = new HashMap<String, Date>();//Class clazz1 = HashMap<String,Date>.class;//complie errorClass clazz2 = HashMap.class;//Class clazz3 = ArrayList<String>.class;//complie errorClass clazz4 = ArrayList.class;Method applyMethod2 = GenericTest.class.getMethod("applyVector2", HashMap.class, Integer.class, Object.class);Type[] types2 = applyMethod2.getGenericParameterTypes();//得到方法上的类型定义,其中包含泛型for(Type type : types2){System.out.println("<---------");if(type instanceof ParameterizedType){ParameterizedType pType = (ParameterizedType)type;System.out.println("rawType:"+pType.getRawType());//this type is in the class fileSystem.out.println("ownerType:"+pType.getOwnerType());System.out.println(Arrays.toString(pType.getActualTypeArguments()));}System.out.println("type class:"+type.getClass());System.out.println("----------->");}//parameterized arguments was stored in the Method Object./* <---------rawType:class java.util.HashMapownerType:null[class java.lang.String, class java.util.Date]type class:class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl-----------><---------type class:class java.lang.Class-----------><---------type class:class sun.reflect.generics.reflectiveObjects.TypeVariableImpl-----------> */}public static void applyVector(Vector<Date> v1){}public static <T> void applyVector2(HashMap<String,Date> h, Integer i, T t){}private static <T> void copy2(T[] dates, T[] strings){}private static <T> void copy1(Vector<T> vector, T[] strings){}private static <T> T autoConvertType(Object obj){return (T)obj;}private static <T> void swap(T[] array, int i, int j){T temp = array[i];array[i] = array[j];array[j] = temp;}private static <T> T add(T i, T j)//泛型定义要加《》,而使用则无须{return null;}private static void printCollection(Collection<?> collection3)//? denote type is unkonw{/*T t = (T)new Object();collection.add(t);*/System.out.println(collection3.size());for(Object obj : collection3){System.out.println(obj);}}private static <T> void printCollection2(Collection<T> collection)//? denote type is unkonw{/*T t = (T)new Object();collection.add(t);*/System.out.println(collection.size());for(Object obj : collection){System.out.println(obj);}}}
-------android培训、java培训、期待与您交流! ----------
- 黑马程序员:基础加强之泛型
- 黑马程序员--加强之泛型
- 黑马程序员-java学习基础加强之泛型
- 黑马程序员——java基础加强之泛型
- 黑马程序员_java基础加强之泛型
- 黑马程序员-Java基础加强之泛型
- 黑马程序员_基础加强之泛型
- 17--黑马程序员--基础加强之泛型
- 黑马程序员--加强之Enumeration
- 黑马程序员--加强之reflection
- 黑马程序员--加强之Anotation
- 黑马程序员--加强之ClassLoader
- 黑马程序员之基础加强之反射
- 黑马程序员_学习日记十六_基础加强之泛型、类加载器
- 黑马程序员—Java基础加强学习笔记之泛型
- 黑马程序员_笔记十六_基础加强之泛型
- 黑马程序员---java基础加强---jdk1.5新特性之泛型
- 黑马程序员——基础加强之 Java5的泛型
- Web应用程序状态管理
- Oracle学习(10)-RMAN备份
- Java:String、StringBuffer和StringBuilder的区别
- iOS - 沙盒文件读写
- hdu3874Necklace(线段树或者BIT + 离线处理)
- 黑马程序员--加强之泛型
- Objective C - Mac打开资源库:
- 黑马程序员--加强之ClassLoader
- Servlet实现方式(如何创建Servlet)
- C++笔记_函数的定义
- php设置编码风格
- iOS - UITableViewCell自适应高度
- 使用 Async 和 Await 的异步编程
- Objective C - 获取磁盘空间: