自学内容网 自学内容网

泛型接口:

1、当实现类不是泛型类,那么接口要明确泛型类型

2、如果实现类是泛型类,那么实现类的泛型标识,至少包含接口的泛型标识

①定义一个泛型接口,接口就是一种抽象方法调用器,类就是变量和方法的存储器

package com.iweb.test;

public interface Generator<T>{
    T getKey();
}

②定义一个双泛型类调用接口

public class Pair<T,E> implements Generator<T>{
    private  T key;
    private  E value;

    public Pair() {
    }

    public Pair(T key, E value) {
        this.key = key;
        this.value = value;
    }

    public void setKey(T key) {
        this.key = key;
    }

    public E getValue() {
        return value;
    }

    public void setValue(E value) {
        this.value = value;
    }

    @Override
    public T getKey() {
        return key;
    }
}

③测试类
 

public class Test1 {
    public static void main(String[] args) {
        Pair<String,Integer> pair= new Pair<>("score",100);
        System.out.println(pair.getKey());
        System.out.println(pair.getValue());

    }
}


原文地址:https://blog.csdn.net/xjdkxnhcoskxbco/article/details/140464176

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!