美文网首页Java基础
Java 常用工具类--泛型

Java 常用工具类--泛型

作者: 磊_5d71 | 来源:发表于2018-09-15 17:08 被阅读0次
    • 不使用泛型
      1、需要强制类型转换
      2、可以添加任意类型,并且编译阶段不会报错,运行时会报错。给程序带来隐患。

    • JDK1.7以后,构造方法中可以省略泛型类型
      List<String> list = new ArrayList<>();

    泛型作为方法的参数

    • Goods类及其子类Book、Shoes、Clothes
    package com.alan.generic;
    
    public abstract class Goods {
        
        public abstract void sell();
    
    }
    
    package com.alan.generic;
    
    public class Book extends Goods {
    
        @Override
        public void sell() {
            System.out.println("sell book!");
    
        }
    
    }
    
    package com.alan.generic;
    
    public class Clothes extends Goods {
    
        @Override
        public void sell() {
            System.out.println("sell clothes!");
    
        }
    
    }
    
    package com.alan.generic;
    
    public class Shoes extends Goods {
    
        @Override
        public void sell() {
            System.out.println("sell shoes!");
    
        }
    
    }
    
    • GoodsSeller实现类,参数为泛型List<? extends Goods>
    package com.alan.generic;
    
    import java.util.List;
    
    public class GoodsSeller {
    
        //通过? extends Goods 泛型定义为 Goods以及继承自它的子类
        public void sellGoods(List<? extends Goods> goods) {
    
            for (Goods g : goods) {
                g.sell();
            }
        }
    
    }
    
    • 测试类
    package com.alan.generic;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class GoodsTest {
    
        public static void main(String[] args) {
            //
            List<Book> bookList = new ArrayList<Book>();
            bookList.add(new Book());
            bookList.add(new Book());
            bookList.add(new Book());
    
            List<Clothes> clothesList = new ArrayList<Clothes>();
            clothesList.add(new Clothes());
            clothesList.add(new Clothes());
            clothesList.add(new Clothes());
    
            List<Shoes> shoesList = new ArrayList<Shoes>();
            shoesList.add(new Shoes());
            shoesList.add(new Shoes());
            shoesList.add(new Shoes());
    
            // 通过泛型作为参数类型,多态方式实现输出
            GoodsSeller gs = new GoodsSeller();
            gs.sellGoods(bookList);
            gs.sellGoods(clothesList);
            gs.sellGoods(shoesList);
        }
    
    }
    

    自定义泛型类

    • 案例1
    package com.alan.generic;
    
    public class NumGeneric<T> {
        
        private T num;
    
        public T getNum() {
            return num;
        }
    
        public void setNum(T num) {
            this.num = num;
        }
        
        //测试主方法
        public static void main(String args[]) {
            NumGeneric<Double> doubleNum = new NumGeneric<Double>();
            doubleNum.setNum(10.0);
            System.out.println("double:"+doubleNum.getNum());
            
            NumGeneric<Integer> integerNum = new NumGeneric<Integer>();
            integerNum.setNum(10);
            System.out.println("integer:"+integerNum.getNum());
        }
    
    }
    
    • 案例2
    package com.alan.generic;
    
    public class TwoNumGeneric <T , X> {
        
        private T num1;
        private X num2;
        
        public void getNum(T num1,X num2) {
            this.num1 = num1;
            this.num2 = num2;
        }
        
        
        public T getNum1() {
            return num1;
        }
        public void setNum1(T num1) {
            this.num1 = num1;
        }
        public X getNum2() {
            return num2;
        }
        public void setNum2(X num2) {
            this.num2 = num2;
        }
        
        public static void main(String args[]) {
            
            TwoNumGeneric<Integer,Float> twoNum = new TwoNumGeneric<>();
            twoNum.getNum(10, 5.5f);
            System.out.println(twoNum.getNum1()+"  "+twoNum.getNum2());
            
        }
    
    }
    

    自定义泛型方法

    package com.alan.generic;
    
    public class GenericMethod {
    
        public <T> void printValue(T t) {
            System.out.println(t);
        }
        
        public static void main(String args[]) {
            
            GenericMethod gm = new GenericMethod();
            gm.printValue("hello");
            gm.printValue(123);
            gm.printValue(10.5f);
            
        }
    }
    

    相关文章

      网友评论

        本文标题:Java 常用工具类--泛型

        本文链接:https://www.haomeiwen.com/subject/zluzgftx.html