Java 常用工具类--泛型

  • 不使用泛型
    1、需要强制类型转换
    2、可以添加任意类型,并且编译阶段不会报错,运行时会报错。给程序带来隐患。

  • JDK1.7以后,构造方法中可以省略泛型类型
    List 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
package com.alan.generic;

import java.util.List;

public class GoodsSeller {

    //通过? extends Goods 泛型定义为 Goods以及继承自它的子类
    public void sellGoods(List 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 bookList = new ArrayList();
        bookList.add(new Book());
        bookList.add(new Book());
        bookList.add(new Book());

        List clothesList = new ArrayList();
        clothesList.add(new Clothes());
        clothesList.add(new Clothes());
        clothesList.add(new Clothes());

        List shoesList = new ArrayList();
        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 {
    
    private T num;

    public T getNum() {
        return num;
    }

    public void setNum(T num) {
        this.num = num;
    }
    
    //测试主方法
    public static void main(String args[]) {
        NumGeneric doubleNum = new NumGeneric();
        doubleNum.setNum(10.0);
        System.out.println("double:"+doubleNum.getNum());
        
        NumGeneric integerNum = new NumGeneric();
        integerNum.setNum(10);
        System.out.println("integer:"+integerNum.getNum());
    }

}
  • 案例2
package com.alan.generic;

public class TwoNumGeneric  {
    
    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 twoNum = new TwoNumGeneric<>();
        twoNum.getNum(10, 5.5f);
        System.out.println(twoNum.getNum1()+"  "+twoNum.getNum2());
        
    }

}

自定义泛型方法

package com.alan.generic;

public class GenericMethod {

    public  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 常用工具类--泛型)