泛型的使用

泛型的使用

Java在实际的使用过程中,会有类型转换的问题,也就存在类型转化异常的安全问题;所以Java提供了泛型来解决这个安全问题。

泛型类

把泛型定义在类型

格式

public class 类名<泛型类型1,...> {}

注意

泛型类型必须是引用类型

测试

public class TestClass {
    public static void main(String[] args) {
        // 创建类时指定什么类型,属性就是什么类型。
        // 把错误提前到编译期显示
        ObjectTest o1 = new ObjectTest<>();
        o1.setT("张三");
        System.out.println(o1.getT());

        ObjectTest o2 = new ObjectTest<>();
        o2.setT(15);
        System.out.println(o2.getT());
    }
}

// 泛型类
class ObjectTest {
    private T t;

    public T getT() {
        return t;
    }

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

泛型方法

把泛型定义在方法上

格式

public <泛型类型> 返回值类型 方法名(泛型类型1 args1,...) {}

测试

public class TestMethod {
    public static void main(String[] args) {
        // 泛型方法与类解绑
        MethodTest m = new MethodTest();
        
        System.out.println(m.show("张三"));
        System.out.println(m.show(13));
    }
}

class MethodTest {
    // 泛型方法
    public  T show(T t) {
        return t;
    }
}
public interface Validator {

    default  T parseJson(String param, Class clz) {

        if (StringUtils.isEmpty(param)) {
            System.out.println("param is empty");
            return null;
        }

        T t = new Gson().fromJson(param, clz);

        if (Objects.isNull(t)) {
            System.out.println("parse param is null");
            return null;
        }

        return t;
    }
}

泛型接口

把泛型定义在接口上

格式

public interface <泛型类型1,...> {}

测试

  1. 实现类知道泛型的类型

    public class TestInter01 {
        public static void main(String[] args) {
            Inter inter = new InterImpl();
            inter.show("张三");
        }
    }
    
    interface Inter {
        void show(T t);
    }
    
    class InterImpl implements Inter {
        @Override
        public void show(String s) {
            System.out.println(s);
        }
    }
    
  1. 实现类不知道泛型的类型(常用)

    public class TestInter02 {
        public static void main(String[] args) {
            Inter inter = new InterImpl<>();
            inter.show("张三");
        }
    }
    
    interface Inter {
        void show(T t);
    }
    
    class InterImpl implements Inter {
        @Override
        public void show(T t) {
            System.out.println(t);
        }
    }
    

泛型通配符

class Animal {}
class Dog extends Animal {}
class Pig extends Animal {}
  • ?

    表示任意类型,如果类型明确,前后类型一致;如果没有明确,就是Object以及任意子类

    // 类型明确
    public class TestTong {
        public static void main(String[] args) {
            Collection o1 = new ArrayList();
            // 错误
            // Collection o2 = new ArrayList();
    
        }
    }
     
    // 类型不明确
    public class TestTong {
        public static void main(String[] args) {
            Collection o1 = new ArrayList();
            Collection o2 = new ArrayList();
            Collection o3 = new ArrayList();
        }
    }
     
    
  • ? extends E

    向下限定,E及其子类

    public class TestTong {
        public static void main(String[] args) {
            // 错误
            // Collection o1 = new ArrayList();
            Collection o2 = new ArrayList();
            Collection o3 = new ArrayList();
            Collection o4 = new ArrayList();
        }
    }
     
    
  • ? super E

    向上限定,E及其父类

    public class TestTong {
        public static void main(String[] args) {
            Collection o1 = new ArrayList();
            Collection o2 = new ArrayList();
            // 错误
            // Collection o3 = new ArrayList();
            // Collection o4 = new ArrayList();
        }
    }
     
    
    
    
    
    
    
    

    你可能感兴趣的:(泛型的使用)