zoukankan      html  css  js  c++  java
  • java_泛型方法使用实例

    //提供两种使用情况,第二种情况定义前者是后者的子类-类型通配方式

    package ming;
    import java.util.ArrayList;
    import java.util.Collection;
    
    public class GenericMethodTest {
    
    	static <T> void fromArraytoCollection(T[] a, Collection<T> c) {
    		for (T o : a) {
    			c.add(o);
    		}
    	}
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//T stand for Object
    		Object[] oa = new Object[100];
    		Collection<Object> co = new ArrayList<Object>();
    		fromArraytoCollection(oa,co);
    		
    		//T stand for Number
    		Integer[] ia = new Integer[100];
    		Float[] fa = new Float[100];
    		Collection<Number> cn = new ArrayList<Number>();
    		
    		fromArraytoCollection(ia,cn);
    		fromArraytoCollection(fa,cn);
    		
    		
    	}
    
    }
    package ming;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    
    public class GenericMethodTest {
    
    	static <T> void fromArraytoCollection(Collection<? extends T> from,
    			Collection<T> to) {
    		for (T ele : from) {
    			to.add(ele);
    		}
    	}
    
    	public static void main(String[] args) {
    		List<Object> ao = new ArrayList<Object>();
    		List<String> as = new ArrayList<String>();
    		// string is subclass of object
    		fromArraytoCollection(as, ao);
    
    	}
    
    }


  • 相关阅读:
    touch命令
    cd命令
    通配符
    速查命令
    一些技巧
    从零开始用 Flask 搭建一个网站(四)
    【老板来了你立刻知道!】人脸识别+手机推送
    React Native 一些事
    React-Native 工程添加推送功能 (iOS 篇)
    集成 jpush
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720460.html
Copyright © 2011-2022 走看看