其他分享
首页 > 其他分享> > 泛型

泛型

作者:互联网

泛型

1.泛型的使用

泛型类

T type

E element

K key

V value

​ 在实例化的时候确定类型

​ 如果不指定,就当成Object来看

​ 静态方法不能使用类的泛型占位符

public class CustomGeneric<T, A, B, C> {
    private String name;
    private T t;
    private A a;
    private B b;

    private C c;

    public C test() {
        return c;
    }
    
        // 静态方法不能使用 类的泛型占位符
    public static void method(A a) {

    }
}
public static void main(String[] args) {
        CustomGeneric customGeneric = new CustomGeneric();
        customGeneric.test()
    }

泛型接口

public interface GenericInter<A, B, C> {
    A method1();

    B method2();

    C method3();
}

1.在实现类时确定泛型借口类型 不指定时默认Object类

public class GenericImpl1 implements GenericInter<String, Integer, Double> {
    @Override
    public String method1() {
        return null;
    }

    @Override
    public Integer method2() {
        return null;
    }

    @Override
    public Double method3() {
        return null;
    }
}

2.实现类时不指定泛型借口类型,在实现类中应带上借口中的泛型,实现类中前表示接口中的泛型,本类的泛型在接口泛型后边

public class GenericImpl1 implements GenericInter {
    @Override
    public Object method1() {
        return null;
    }

    @Override
    public Object method2() {
        return null;
    }

    @Override
    public Object method3() {
        return null;
    }
}
public class GenericImpl2<A, B, C,D> implements GenericInter<A, B, C> {
    @Override
    public A method1() {
        return null;
    }

    @Override
    public B method2() {
        return null;
    }

    @Override
    public C method3() {
        return null;
    }
}

测试类

    public static void main(String[] args) {
        GenericImpl2<Integer, Double, String, Float> genericImpl2 = new GenericImpl2<>();
        Integer integer = genericImple2.method1();
        ArrayList<String> objects = new ArrayList<>();
    }

泛型方法

​ 泛型方法只能在本类中使用

​ 泛型类型由实际传参时决定

​ 静态方法可以使用泛型

public class GenericMethod {
    //只在本方法中使用
    public static void main(String[] args) {
        GenericMethod genericMethod = new GenericMethod();
        //类型由调用方法的时候传参决定
        Integer integer = genericMethod.method1(1);
        String s = genericMethod.method1("qwer");
    }
    public <E>  E method1(E e){
        System.out.println("====");
        return e;
    }
    //静态方法中可以使用泛型  静态类中不可以
    public static <A> void method2(A a){

    }
}

测试类

  public static void genericMethod() {
        GenericMethod genericMethod = new GenericMethod();
        String aaa = genericMethod.method1("aaa");
        Integer integer = genericMethod.method1(1);
        Double aDouble = genericMethod.method1(6.6);
        GenericMethod.method2("aa");
    }

2.泛型通配符及上下限

为了体现出多态,在用泛型的地方需要实现泛型上限

//动物类
public class Animal {
    public void eat(){

    }
}
//狗类  继承动物类
public class Dog extends Animal {
    @Override
    public void eat(){
        System.out.println("狗喜欢吃骨头");
    }
}
//猫类  继承动物类
public class Cat extends Animal {
    public void eat(){
        System.out.println("猫喜欢吃鱼");
    }
}
//喂养者类
public class Feeder {
    //? 泛型通配符  泛型的上限  继承animal的子类或animal
   public void feed(ArrayList<? extends Animal> animals){
     for (Animal animal : animals){
        animal.eat();
     }
   }
    //下限:<? super Animal>只能传Animal的父类
   public  void feed2(ArrayList<?super Animal> animals){
       for (Object animal:animals){
       }
   }
//测试类
  public class FeederTest {
    @Test
    public void feed() {
        ArrayList<Animal> animals = new ArrayList<>() ;
        animals.add(new Dog());
        animals.add(new Cat());
        Feeder feeder = new Feeder();
        ArrayList<Dog> dogs = new ArrayList<>();
        dogs.add(new Dog());
        dogs.add(new Dog());
        feeder.feed(dogs);
    }
}

3.泛型擦除

@Test
    public void method1() {
        ArrayList<Dog> dogs = new ArrayList<>();
        dogs.add(new Dog());
        //泛型擦除
        List list = dogs;
        list.add(new Cat());
    }

标签:return,method1,void,泛型,new,public
来源: https://blog.csdn.net/weixin_45636142/article/details/115530480