zoukankan      html  css  js  c++  java
  • 泛型 和 集合的结合使用

    Java泛型是jdk1.5中引入的一个新特性,其本质是参数化类型,把类型作为参数传递。

    常见形式有泛型类、泛型接口、泛型方法

    语法:

    <T,,,,,,,,> T称为类型占位符,表示一种引用类型,T表示的是type,其实这里写什么都是可以的。

    *好处:*

    (1)提高代码的重用性

    (2)防止类型转换异常,提高代码的安全性。

    *泛型集合*

    概念:参数化类型、类型安全的集合,强制集合元素的类型必须一致

    特点:编译时即可检查,而非运行时抛出异常

       访问时,不必类型转换(拆箱)

      不同泛型之间引用不能相互赋值,泛型不存在多态。

    练习代码

    package com.genericlist;
    
    import com.practise.Student;
    import com.sun.media.sound.SoftTuning;
    
    import java.util.ArrayList;
    import java.util.Collection;
    
    /**
     * 泛型集合的使用 ArrayList
     */
    public class TestArrayListGeneric {
        public static void main(String[] args) {
            //创建集合
            ArrayList<String> arrayList = new ArrayList<String>();
    //         * 1.添加元素
            arrayList.add("apple");
            arrayList.add("bananer");
            arrayList.add("Durian");//表示榴莲
            System.out.println("=========遍历集合==========");
            for (String s : arrayList) {
                System.out.println(s);
            }
            System.out.println("=========遍历集合==========");
            ArrayList<Student> stuList = new ArrayList<Student>();
            Student stu1 = new Student("张三", 18);
            Student stu2 = new Student("李四", 19);
            Student stu3 = new Student("王五", 20);
            stuList.add(stu1);
            stuList.add(stu2);
            stuList.add(stu3);
            for (Student student : stuList) {
                System.out.println(student);
            }
        }
    }
    
    

    运行结果

    =遍历集合==

    apple

    bananer

    Durian

    =遍历集合==

    Student{name='张三', age=18}

    Student{name='李四', age=19}

    Student{name='王五', age=20}

    *泛型类*

    首先,创建一个泛型类

    package com.practise;
    
    /**
     * 泛型类
     * 语法:类名<T>
     * T是类型占位符,表示一种引用数据类型,如果编写多个使用逗号隔开
     */
    public class MyGeneric<T> {
        //使用泛型
        //1.创建变量
        T t;
    
        // T t=new T();注意:这里是不能创建的
        //2.泛型作为方法的参数
        public void show(T t) {
            System.out.println(t);
        }
    
        //3.泛型作为方法的返回值
        public T getT() {
            return t;
        }
    
    }
    
    

    创建测试类

    /**
     * 测试泛型类
     */
    public class TestGeneric {
        public static void main(String[] args) {
            //使用泛型类创建对象
            //注意:1.泛型只能使用引用类型,2.不同泛型类型对象之间不能相互赋值
            MyGeneric<String> stringMyGeneric = new MyGeneric<String>();
            stringMyGeneric.t="hello";
            stringMyGeneric.show("大家好");
            System.out.println(stringMyGeneric.getT());
    
            MyGeneric<Integer> integerMyGeneric = new MyGeneric<Integer>();
            integerMyGeneric.t=100;
            integerMyGeneric.show(200);
            System.out.println(integerMyGeneric.getT());
        }
    }
    
    

    运行结果:

    大家好

    hello

    200

    100

    *泛型方法的使用*

    首先创建一个泛型方法

    package com.practise;
    
    /**
     *
     * 泛型方法
     * 语法:<T> 返回值类型
     * 注意,我们的类型还是一个普通的类,只是方法和普通的方法有一点不同。
     */
    public class MyGenericMethod {
        //泛型方法 这种是没有返回值的
    /*    public <T> void show(T t){
            System.out.println("泛型方法:"+t);
    
        }*/
        //泛型方法 这种是有返回值的
        public <T> T show(T t){
            System.out.println("泛型方法:"+t);
            return t;
        }
        //泛型静态方法 只需要在泛型前面加上static就可以了 这种是有返回值的
     /*   public static <T> T show(T t){
            System.out.println("泛型方法:"+t);
            return t;
        }*/
    }
    
    

    创建测试类:

    package com.practise;
    
    
    /**
     * 测试泛型类
     */
    public class TestGeneric {
        public static void main(String[] args) {
            //使用泛型类创建对象
            //注意:1.泛型只能使用引用类型,2.不同泛型类型对象之间不能相互赋值
    //        MyGeneric<String> stringMyGeneric = new MyGeneric<String>();
    //        stringMyGeneric.t="hello";
    //        stringMyGeneric.show("大家好");
    //        System.out.println(stringMyGeneric.getT());
    //
    //        MyGeneric<Integer> integerMyGeneric = new MyGeneric<Integer>();
    //        integerMyGeneric.t=100;
    //        integerMyGeneric.show(200);
    //        System.out.println(integerMyGeneric.getT());
    //泛型接口的测试
    //        MyInterface myInterface = new MyInterfaceImpl();
    //        myInterface.server("哈哈哈");
    //
    //        MyInterfaceImpl2 myInterfaceImpl2 = new MyInterfaceImpl2();
    //        myInterfaceImpl2.server("i miss you");
    //        //泛型方法的测试
            MyGenericMethod myGenericMethod = new MyGenericMethod();
            myGenericMethod.show("张家口加油");
            myGenericMethod.show(2000);
            myGenericMethod.show(3.1415926);
        }
    }
    
    

    返回值:

    泛型方法:张家口加油

    泛型方法:2000

    泛型方法:3.1415926

    *泛型接口*

    package com.practise;
    
    /**
     * 泛型接口
     * 语法:接口名<T>
     * 注意:不能使用泛型静态常量
     *
     */
    public interface MyInterface<T> {
        String name="张三";
        T server(T t);
    }
    
    /*
    接口里面可以写的东西
    1.成员变童其实是常童,格式:
    [public] [static] [final] 数据类型 常置名称 = 数据值;
    注意:常量必须进行赋值,而且一旦赋值不能改变。
    常量名称完全大写,用下划线进行分隔。
    
    2.接口中最重要的就是抽象方法,格式:
    [public] [abstract] 返回值类型 方法名称(参数列表〉;
    注意:实现类必须覆盖重写接口所有的抽象方法,除非实现类是抽象类。
     */
    
    

    *创建接口实现类*

    package com.practise;
    
    /**
     * 泛型接口的实现类
     */
    public class MyInterfaceImpl implements MyInterface<String> {
    
        public String server(String s) {
            System.out.println(s);
            return s;
        }
    }
    
    

    *或者*

    package com.practise;
    
    /**
     * 泛型接口的实现类
     * 实现类是什么类型,接口就是什么类型,这样就能够做到在实例化对象的时候指定接口的类型了
     */
    public class MyInterfaceImpl2<T>  implements MyInterface<T> {
    
        public T server(T t) {
            System.out.println(t);
            return t;
        }
    }
    
    

    *测试类*

    package com.practise;
    
    
    /**
     * 测试泛型类
     */
    public class TestGeneric {
        public static void main(String[] args) {
    //泛型接口的测试
            MyInterface myInterface = new MyInterfaceImpl();
            myInterface.server("哈哈哈");
    
            MyInterfaceImpl2 myInterfaceImpl2 = new MyInterfaceImpl2();
            myInterfaceImpl2.server("i miss you");
    
        }
    }
    
    

    *运行结果*

    哈哈哈

    i miss you

  • 相关阅读:
    spring 09-Spring框架基于QuartZ实现调度任务
    spring 08-Spring框架Resource资源注入
    spring 07-Spring框架Resource读取不同资源
    spring 06-Spring框架基于Annotation的依赖注入配置
    html 默认跳转
    poi 设置样式
    支付宝扫码支付回调验证签名
    构造器初始化
    cxf webservice
    CSS3 border-image 属性
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/14349046.html
Copyright © 2011-2022 走看看