• java001-泛型


    泛型出现的意义:

    为编码阶段的不确定性和转化做视觉设计

    将运行期遇到的问题转移到编译期,省去了强转的麻烦

    package com.xiaolin.basic;
    
    /**
     * 泛型:将运行期遇到的问题转移到了编译期,省去了强转的麻烦
     * val1,val2
     *  @author: xiaolin
     *  @Date: 2021/6/11 22:49
     *  @Description:
     */
    public class LocalPair<K,V> {
        private K k;
        private V v;
    
        public LocalPair(K k, V v) {
            this.k = k;
            this.v = v;
        }
    
    
        public K getK() {
            return k;
        }
    
        public V getV() {
            return v;
        }
    }
    
    package com.xiaolin.basic;
    
    import java.util.Date;
    
    public class LocalPairApp {
        public static void main(String[] args) {
            LocalPair<String,String> pair = foo1();
    
            String k = pair.getK();
            String v = pair.getV();
    
            String[] arrs = k.split(",");
            String[] arrs2 = v.split(",");
    
            for (String s:arrs){
                System.out.println(s);
            }
    
            for (String b:arrs2){
                System.out.println(b);
            }
        }
    
    
        public static LocalPair<String,String> foo1(){
            LocalPair<String,String> pair1 = new LocalPair<>("abc,rer","bcd,oiu");
    
            return pair1;
        }
    
    
        public static LocalPair<String,Integer> foo2(){
            LocalPair<String,Integer> pair2 = new LocalPair<>("bdsd",123);
    
            return pair2;
        }
    
    
        public static LocalPair<String, Date> foo3(){
            LocalPair<String,Date> pair3 = new LocalPair<>("bdsd",new Date());
    
            return pair3;
        }
    }
    

      

      

  • 相关阅读:
    iOS ARC编译器规则和内存管理规则
    Servlet与JSP的关系
    传统javabean与spring中的bean的区别
    servlet学习笔记
    JAVA里面"=="和euqals的区别
    java垃圾回收
    java中初始化块、静态初始化块和构造方法
    抽象类与接口的区别
    Servlet 与 CGI 的比较
    spring的事务传播特性
  • 原文地址:https://www.cnblogs.com/cjxxl1213/p/14877039.html
Copyright © 2020-2023  润新知