zoukankan      html  css  js  c++  java
  • JAVA泛型的基本使用

    Java1.5版本号推出了泛型,尽管这层语法糖给开发者带来了代码复用性方面的提升,可是这只是是编译器所做的一层语法糖,在真正生成的字节码中,这类信息却被擦除了。

    笔者发现非常多几年开发经验的程序猿,依旧不善于使用Java泛型,本文将从Java泛型的基本使用入手,在今后的多篇博文里。对泛型的使用做个总结。本文不会深入Java泛型的实现原理。仅仅会介绍Java泛型的使用。

    实验准备

      首先须要创建一个类继承体系。以商品为例,每种商品都有主要的名称属性。在大数据应用中,数据表和服务都能够作为商品,表有行数属性,服务有方法属性。实现如代码清单1所看到的。

    代码清单1

    class Auction {
    
        private String name;
    
        public Auction(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public String toString() {
            return name;
        }
    
    }
    
    class Table extends Auction {
        
        private Integer rowNum;
    
        public Table(String name, Integer rowNum) {
            super(name);
            this.rowNum = rowNum;
        }
    
        public Integer getRowNum() {
            return rowNum;
        }
    
        public void setRowNum(Integer rowNum) {
            this.rowNum = rowNum;
        }
        
        public String toString() {
            return super.toString() + ", rowNum :" + rowNum;
        }
        
    }
    
    class Service extends Auction {
    
        private String method;
        
        public Service(String name, String method) {
            super(name);
            this.method = method;
        }
    
        public String getMethod() {
            return method;
        }
    
        public void setMethod(String method) {
            this.method = method;
        }
        
        public String toString() {
            return super.toString() + ", method :" + method;
        }
        
    }

    实验:泛型最基本使用

      如今为了演示泛型的使用,还是以商品为例。数据表本身仅仅是Hbase中的一张表,而服务是执行于Hadoop Yarn之上的一个数据服务,比方说是Strom、Spark或者Oozie。不管是数据表还是服务,它本身并非商品,要成为商品。必须有个装饰器将它包装为商品,如今我们用泛型实现一个简单的装饰器。见代码清单2所看到的。

    代码清单2

    class Decorator<T> {
        
        /**
         * 
         * 描 述:Exp1使用<br/>
         * 作 者:jiaan.gja<br/>
         * 历 史: (版本号) 作者 时间 凝视 <br/>
         * @param itemList
         */
        public void doDecorate(List<T> itemList) {
            for(int i = 0; i < itemList.size(); i++) {
                System.out.println(itemList.get(i));
            }
        }
        
        /**
         * 
         * 描 述:Exp1使用<br/>
         * 作 者:jiaan.gja<br/>
         * 历 史: (版本号) 作者 时间 凝视 <br/>
         * @param itemList
         * @param t
         */
        public void addDecorate(List<T> itemList, T t) {
            itemList.add(t);
        }
        
    }

    如今我们能够使用Decorator给List加入或者打印不论什么类型了,如代码清单3所看到的。

    代码清单3

    /**
     * 
     * 类 名: Exp1<br/>
     * 描 述: 泛型最基本使用<br/>
     * 作 者: jiaan.gja<br/>
     * 创 建: 2015年8月13日<br/>
     *
     * 历 史: (版本号) 作者 时间 凝视
     */
    class Exp1 {
        public static void main(String[] args) {
    
            Decorator<Auction> decoratorA = new Decorator<Auction>();
            List<Auction> listA = new ArrayList<Auction>();
            Auction auctionOne = new Auction("auctionOne");
            Auction auctionTwo = new Auction("auctionTwo");
            decoratorA.addDecorate(listA, auctionOne);
            decoratorA.addDecorate(listA, auctionTwo);
            decoratorA.doDecorate(listA);
            
            Decorator<Table> decoratorB = new Decorator<Table>();
            List<Table> listB = new ArrayList<Table>();
            Table tableOne = new Table("tableOne", 10);
            Table tableTwo = new Table("tableTwo", 20);
            decoratorB.addDecorate(listB, tableOne);
            decoratorB.addDecorate(listB, tableTwo);
            decoratorB.doDecorate(listB);
            
            Decorator<Service> decoratorC = new Decorator<Service>();
            List<Service> listC = new ArrayList<Service>();
            Service serviceOne = new Service("serviceOne", "methodOne");
            Service serviceTwo = new Service("serviceTwo", "methodTwo");
            decoratorC.addDecorate(listC, serviceOne);
            decoratorC.addDecorate(listC, serviceTwo);
            decoratorC.doDecorate(listC);
            
        }
    }

    遗留问题

    假设想要Auction用于Decorator,必需要声明Decorator<Auction>;Service用于Decorator,也必须声明Decorator<Service>。因为Table是Auction的子类型,我们自然想将Table也能用于Decorator<Auction>。就像以下这样:

    decoratorA.doDecorate(listB);


    listB的泛型Table的确是decoratorA的泛型Auction的子类型,可是编译器会报错。说明不同意这样的语法。

    怎样解决问题?请看下一篇《Java泛型的协变》

  • 相关阅读:
    Android Media Playback 中的MediaPlayer的用法及注意事项(二)
    Android Media Playback 中的MediaPlayer的用法及注意事项(一)
    34. Search for a Range
    33. Search in Rotated Sorted Array
    32. Longest Valid Parentheses
    31. Next Permutation下一个排列
    30. Substring with Concatenation of All Words找出串联所有词的子串
    29. Divide Two Integers
    28. Implement strStr()子串匹配
    27. Remove Element
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7114825.html
Copyright © 2011-2022 走看看