Java transient关键字的使用
2016-02-23 09:59
417 查看
JAVA泛型大体分为:基本泛型和通配符泛型,在大多数项目下一般使用基本泛型的概率会大一些,此文主要就是对最近学习的泛型进行的一个总结,不足的望给予批评与指导!下例子
案例1:
案例2:
案例1:
public class GenericTest { /*在JAVA1.4中的集合应用是无泛型的,我们可以向内部传入各种种类的值*/ public void Generic14(){ ArrayList co = new ArrayList(); co.add(1); co.add(1L); co.add("aaa"); /*但是我们需要在获取的时候对其进行强制类型转换*/ int i = (Integer)co.get(0); System.out.println(i); } /*JDK1.5中添加了泛型关键字为<>(如<String>,其中放置的基本数据类型)这样有效的减少强制转换*/ public void Generic15() throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{ ArrayList<Integer> cos = new ArrayList<Integer>(); cos.add(1); cos.add(2); /*因为经过泛型所以cos.add("abc");不能被编译器所通过,但是泛型的样式在编译过后将被剔除仍为ArrayList形式*/ /*以下为反射的例子,去做验证,利用反射的方法调用ArrayList的add方法向cos对象中插入字符串abc*/ Method method = ArrayList.class.getMethod("add", Object.class); // 对象 参数值 method.invoke(cos, "abc"); //遍历cos对象 for(Object c : cos){ System.out.println(c); } } /*例子2*/ public void GenericlA(){ List<Integer> A = new ArrayList<Integer>(); List<String> B = new ArrayList<String>(); //泛型在编译时已经被去除并不带入到运行过程 System.out.println(A.getClass()==B.getClass()); } public void Connection(){ //Collection父类 Collection c = new ArrayList(); //Vector类型相当于Arraylist内部进行了syn的处理,效率会相对Array的类型要底 Vector<String> d = new Vector(); } public static void main(String[] args) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { GenericTest genericTest = new GenericTest(); // genericTest.Generic14(); // genericTest.Generic15(); // genericTest.GenericlA(); genericTest.GAll(); } public static void GAll(){ //map的迭代 HashMap<String, Integer> hashMaps = new HashMap<String, Integer>(); hashMaps.put("dhy", 11); hashMaps.put("slb",5); //但是map型不能直接进行迭代必须转化为set类型才能 Set<Map.Entry<String, Integer>> entrySet = hashMaps.entrySet(); for (Entry<String, Integer> entry : entrySet) { System.out.println(entry.getKey()+" : "+entry.getValue()); } } }
案例2:
public class GenericWTest { public static void printCollection (Collection<Object> collection){ for(Object c : collection){ System.out.println(c); } } public static void main(String[] args) { ArrayList<String> test = new ArrayList<String>(); Collection<String> cn = new ArrayList<String>(); /* 本身定义的是Collection<Object> * 传入的是ArrayList(Collection的子类型)<String(Object的子类型)> * 但是却说类型不对,原因在于Collecttion<Object>这样定义后就是要传入<object>的值 * 所以在泛型中没有父类与子类之分,所以要用下面的方法 */ ArrayList<Object> test2 = new ArrayList<Object>(); test.add("a"); test.add("b"); printCollection(test2); String[] strs = new String[]{"aaa","bbb","ccc"}; ArrayList lists = new ArrayList(); lists = (ArrayList) copy1(strs, lists); for (Object list : lists) { System.out.println(list); } // 泛型的追溯特性,String和Date的都是Serializable的子类 copy(new String[10], new Date[10]); // 泛型的传播特性,其中为Date类型() // copy1(new String[10],new ArrayList<Date>()); } /*为此推出了?这个通配符*/ public static void printCollection2 (Collection<?> collection){ /*但是引入这个通配符后,不能对这样的Collection对象去调用与参数有关的方法*/ /*collection.add("aa");这样的语句是报错的,因为他不能确定泛型是什么类型的*/ for(Object c : collection){ System.out.println(c); } } //13:servlet jsp jmail jdbc jndi xml public static void ClassTest(){ Class<?> a = null; } // 泛型方法 public static <T> T[] copy(T[] forms,T[] to){ return null; } public static <T> Collection<T> copy1(T[] froms,Collection<T> to){ for(T yuan : froms){ to.add(yuan); } return to; } }
相关文章推荐
- java 图片质量压缩
- [Spring]02_Spring框架和MVC原理
- java实现定时任务的三种方法
- java spring mvc时间String转Date(二)
- Java transient关键字使用
- myeclipse添加server library
- Java生成验证码工具
- Java 反射机制 .class .getClass() Class.forName("") .TYPE int void
- Eclipse修改svn地址
- 使用SpringSecurity3用户验证(异常信息,验证码)
- JAVA_面向对象_练习_封装_02_EncapsulationDaemon02.java
- JAVA_面向对象_练习_封装_02_EncapsulationDaemon02.java
- Struts2动态方法调用
- 【转】java中三个类别加载器的关系以及各自加载的类的范围
- JAVA_面向对象_练习_封装_01_EncapsulationDaemon01.java
- JAVA_面向对象_练习_封装_01_EncapsulationDaemon01.java
- JRE与JDK的区别
- IOC
- JDK1.8的一些改动
- 在Eclipse中打开文件夹