栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java的泛型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java的泛型

泛型

public class GenericDemo {
    public static void main(String[] args) {
        Collection collection =new ArrayList();

        //添加元素
        collection.add("java");
        collection.add(12);

        System.out.println("-----------------");
        System.out.println(collection);
        for (Object o: collection) {
            System.out.println(o);
        }

        System.out.println("--------------------");
        Iterator iterator=collection.iterator();
        while (iterator.hasNext()){
            Object next = iterator.next();
            System.out.println(next);
        }
    }
}

这是没有使用泛型的情况,可以发现任何对象都可以装进去,但是一旦我们修改为

Collection collection =new ArrayList();

就表示只能接收String对象,而不是所有都能装进去(12就不能装进去,会报错)

泛型类

public class Generic {
    private T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}

//测试类
public class GenericDemo {
    public static void main(String[] args) {
       Generic g1=new Generic<>();
       g1.setT("王五");
       System.out.println(g1.getT());

        Generic g2=new Generic<>();
        g2.setT(12);
        System.out.println(g2.getT());
    }
}

泛型方法
public class Generic {
    public void show(T t){
        System.out.println(t);
    }
}

//测试类
public class GenericDemo {
    public static void main(String[] args) {
       Generic generic=new Generic<>();
       generic.show("zhangsan");

       Generic generic1=new Generic<>();
       generic1.show(12);
    }
}

//public class Generic {
//    public void show(T t){
//        System.out.println(t);
//    }
//}

public class Generic {
    public void show(T t){
        System.out.println(t);
    }
}
//测试类
public class GenericDemo {
    public static void main(String[] args) {
       Generic generic=new Generic();
        generic.show("zhangsan");
        generic.show(15);
    }
}

泛型接口

public interface Generic {
    void show(T t);
}

public class GenericImpl implements Generic{
    @Override
    public void show(T t) {
        System.out.println(t);
    }
}

public class test {
    public static void main(String[] args) {
        GenericImpl generic=new GenericImpl();
        generic.show("wangwu");

        Generic generic1=new GenericImpl<>();
        generic1.show(12);
    }
}

类型通配符

public class GenericDemo {
    public static void main(String[] args) {
        //类型通配符:?
        List list=new ArrayList();
        List list1=new ArrayList();
        List list2=new ArrayList();
        System.out.println("------------------");
        
        //类型通配符的上限:
        List list3=new ArrayList<>();
        List list4=new ArrayList();
        
        //类型通配符的下限:
        List list5=new ArrayList();
        List list6=new ArrayList();
    
        
    }
}
 
可变参数 

public class test2 {
    public static void main(String[] args) {
        System.out.println(sum(10, 20, 30));
        System.out.println(sum(10, 20, 30,40));
        System.out.println(sum(10, 20, 30,50));

    }
    public static int sum(int... a){
        int num=0;

        for (Integer i: a){
            num+=i;
        }
        return num;
    }
}

可变参数的使用

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号