getGenericInterfaces的使用

创建基础接口

public interface BaseMapper<T> {
    List<T> query(T t);
}

创建继承的子接口

public interface AbcMapper extends BaseMapper<Abc> {
    Abc queryList(Abc abc);
}

创建一个getter,setter类

public class Abc {
    private String val;

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }

}

运行 getGenericInterfaces 数据的信息输出:

public static void main(String[] args) {
    Class<?> clazz = AbcMapper.class;

    Type[] types = clazz.getGenericInterfaces();

    for (Type type : types) {
        if (type instanceof ParameterizedType) {
            ParameterizedType t = (ParameterizedType)type;
            System.out.println("t: " + t.getTypeName());

            Class<?> returnType = (Class)t.getActualTypeArguments()[0];

            System.out.println("r: " + returnType.getName());
        }
    }
}

程序运行输出 :
t: com.test.source.dao.BaseMapper
r: com.test.source.model.Abc

创建 SecondMapper接口;

public interface SecondMapper<T> {
    int insert(T t);
}

调整AbcMapper;

public interface AbcMapper extends BaseMapper<Abc>, SecondMapper<Abc> {
    Abc queryList(Abc abc);
}

结果有四个输出;
t: com.test.source.dao.BaseMapper
r: com.test.source.model.Abc
t: com.test.source.dao.SecondMapper
r: com.test.source.model.Abc

欢迎您的到来,感谢您的支持!

为您推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注