【6】Java泛型

一、什么是泛型

1、泛型其实就是在定义类、接口、方法的时候不局限地指定某一种特定类型,而让类、接口、方法的调用者来决定具体使用哪一种类型的参数。

​2、比如一个水杯生产的时候不用指定它将来干什么?而是由将来的使用者决定放入什么。

3、假定我们有这样一个需求:写一个排序方法,能够对整型数组、字符串数组甚至其他任何类型的数组进行排序,该如何实现?

答案是可以使用 Java 泛型。

使用 Java 泛型的概念,我们可以写一个泛型方法来对一个对象数组排序。然后,调用该泛型方法来对整型数组、浮点数数组、字符串数组等进行排序。

二、泛型的使用

List<String>.list=new ArrayList<String>();

List<String> list=new ArrayList<>();//java SE7以后的版本中,构造方法中可以省略泛型类型

三、泛型作为方法参数

这里用案例举例

//父类、抽象类
public abstract class Goods {
    //抽象方法
    public abstract void sell();
}
父类Goods
Shoes
//继承父类
public class Book extends Goods {
//重写抽象方法
    @Override
    public void sell() {
        System.out.println("sell books");

    }

}
Book
//继承父类
public class Clothes extends Goods {
    //重写抽象方法
    @Override
    public void sell() {
        System.out.println("sell clothes");
    }

}
Clothes
mport java.util.List;
//买商品的方法
public class GoodsSeller {
    //泛型作为参数的方法:传进来的参数是一个泛型
    //加上通配符?表示允许Goods及其子类
    public void sellGoods(List<? extends Goods> goods){//(List<Goods> goods)
        //调用集合中的sell方法
        //增强for循环遍历list,输出信息
        for(Goods g:goods){
            g.sell();
        }
    }
}
import java.util.ArrayList;
import java.util.List;

public class GoodsTest {

    public static void main(String[] args) {
        //定义book相关的List
        List<Book> books=new ArrayList<Book>();
        books.add(new Book());
        books.add(new Book());
        //定义chothes相关的List
        List<Clothes> clothes=new ArrayList<Clothes>();
        clothes.add(new Clothes());
        clothes.add(new Clothes());
        //定义shoes相关的List
        List<Shoes> shoes=new ArrayList<>();
        shoes.add(new Shoes());
        shoes.add(new Shoes());

        //对象 调用 方法
        GoodsSeller goodsSeller=new GoodsSeller();
        goodsSeller.sellGoods(books);
        goodsSeller.sellGoods(clothes);
        goodsSeller.sellGoods(shoes);
    }

}

四、自定义泛型类

 自定义泛型相当于把类进行了参数化,具体类型由传入的参数来决定。泛型用的符号是任意的,一般采用T或E。

具体地,对于类 public class NumGeneric<T>{},泛型T表示NumGeneric类会使用T作为类中某些成员变量的类型、方法参数的类型或方法返回值的类型等。

至于T具体会是什么类,则由实例化NumGeneric对象时传入的泛型参数来决定,如下第11行代码则是以Integer类作为具体的泛型类型。

 1 public class NumGeneric<T> {
 2     private T num;
 3     public T getNum(){
 4         return num;
 5     }
 6     public void setNum(T num){
 7         this.num=num;
 8     }
 9     //测试
10     public static void main(String[] args){
11         NumGeneric<Integer> intNum=new NumGeneric<>();
12         intNum.setNum(10);
13         System.out.println("Integer:"+intNum.getNum());
14         
15         NumGeneric<Float> floatNum=new NumGeneric<>();
16         floatNum.setNum(5.0f);
17         System.out.println("Float:"+floatNum.getNum());
18     }
19 }

五、自定义泛型方法

1、泛型可以声明在类的声明中,这些类可称为泛型类。

如对于类 public class NumGeneric<T,X>{},泛型T,X表示NumGeneric类会使用T和X作为类中某些成员变量的类型、方法参数的类型或方法返回值的类型等。

2、泛型也可以直接声明在非泛型类中的方法声明中,这些方法可称为泛型方法。如对于

public class GenericMethod{

--public <T extends Number> void println(T t){...}

}

而言,即是将泛型声明在非泛型类中的方法声明中,其中<T extends Number>表示传入的泛型T必须是Number类及其子类对象,写在这里是对传入的泛型T的一种限定。

public class GenericMethod {
    public <T > void printValue(T t){
//    <T extends Number> 则只能传入number及其子类
        System.out.println(t);
    }
    
    public static void main(String[] args){
        GenericMethod gm=new GenericMethod();
        
        gm.printValue(123);
        gm.printValue("hello");
        gm.printValue(5.0f);
    }
}

猜你喜欢

转载自www.cnblogs.com/haifeima/p/13160649.html