泛型筆試題

public class App {

    public void print(Collection<?> coll){
        System.out.println("I am Collection!!!");
    }

    public void print(ArrayList<?> arr){
        System.out.println("I am ArrayList!!!");
    }

    public void print(Set<?> arr){
        System.out.println("I am Set!!!");
    }

    public void print(HashSet<?> arr){
        System.out.println("I am HashSet!!!");
    }

    public static void main(String[] args) {
        App app = new App();
        Collection coll = new ArrayList<>(Arrays.asList("1","2","3"));
        app.print(coll);
        Set set = new HashSet(Arrays.asList("1","2","3"));
        app.print(set);
        //運行結果:
        //I am Collection!!!
        //I am Set!!!
    }
}
相關文章
相關標籤/搜索