泛型的简单应用 --《JAVA编程思想》 52

泛型:指某种不具体的类型。泛型只有在赋值的时候才确定具体类型,增强了代码的适用性。

今天和大家分享泛型的常见应用场景。

1.在接口中使用泛型

在接口使用泛型,需在类名后面定义泛型 T ,并用 <> 包裹起来。

不同的类实现接口时,可以指定不同的具体类型。

public interface GeneratorFactory<T> {
    
    
    T newInstance();
}
public class Coffee {
    
    }
public class CoffeeFactory implements GeneratorFactory<Coffee> {
    
    
    @Override
    public Coffee newInstance() {
    
    
        return new Coffee();
    }
}

public class Fruit {
    
    }
public class FruitFactory implements GeneratorFactory<Fruit> {
    
    
    @Override
    public Fruit newInstance() {
    
    
        return new Fruit();
    }
}

2.在类中使用泛型

在类中使用泛型与接口中基本一致,传入的泛型可在类中作为私有域进行使用。

public class Plate<T> {
    
    

    private T item;

    public Plate(T item) {
    
    
        this.item = item;
    }

    @Override
    public String toString() {
    
    
        return item.toString();
    }
    
}
public class Fruit {
    
    

    @Override
    public String toString() {
    
    
        return "Fruit{}";
    }

}
public class Coffee {
    
    

    @Override
    public String toString() {
    
    
        return "Coffee{}";
    }

}
public class PlateTest {
    
    

    public static void main(String[] args) {
    
    
        Plate<Coffee> coffeePlate = new Plate<>(new Coffee());
        System.out.println(coffeePlate.toString());
        Plate<Fruit> fruitPlate = new Plate<>(new Fruit());
        System.out.println(fruitPlate.toString());
    }

}
Coffee{
    
    }
Fruit{
    
    }

3.在方法中使用泛型

在方法中使用泛型,不仅需要将方法中的参数定义为泛型,还需要在方法的返回值前进行声明。

public class GenericMethods {
    
    

    public <T> void getClassName(T t) {
    
    
        System.out.println(t.getClass().getName());
    }

    public static void main(String[] args) {
    
    
        GenericMethods gm = new GenericMethods();
        gm.getClassName("abc");
        gm.getClassName(123);
        gm.getClassName(1.2F);
        gm.getClassName(2.4);
        gm.getClassName(2300L);
    }
    
}
java.lang.String
java.lang.Integer
java.lang.Float
java.lang.Double
java.lang.Long

4.定义多个泛型

这里可能有同学会说,假设我同时需要定义多个泛型,该怎么办呢?

很简单,在 <> 中接着声明即可。

4.1 在接口中定义多个泛型

public interface GeneratorFactoryExpand<T, E> {
    
    

    T newInstanceT();

    E newInstanceE();

}

4.2 在类中定义多个泛型

public class PlateExpand<T, E> {
    
    

    private T item;

    private E element;

    public PlateExpand(T item, E element) {
    
    
        this.item = item;
        this.element = element;
    }

    @Override
    public String toString() {
    
    
        return "PlateExpand{" +
                "item=" + item +
                ", element=" + element +
                '}';
    }
    
}

4.3 在方法中定义多个泛型

public class GenericMethodsExpand {
    
    

    public <T, E> void getClassName(T t, E e) {
    
    
        System.out.println(t.getClass().getName());
        System.out.println(e.getClass().getName());
    }

}

本次分享至此结束,希望本文对你有所帮助,若能点亮下方的点赞按钮,在下感激不尽,谢谢您的【精神支持】。

若有任何疑问,也欢迎与我交流,若存在不足之处,也欢迎各位指正!

猜你喜欢

转载自blog.csdn.net/BaymaxCS/article/details/120252461