Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-如何创建调用接口方法的泛型类?_Java_Generics_Interface - Fatal编程技术网

Java-如何创建调用接口方法的泛型类?

Java-如何创建调用接口方法的泛型类?,java,generics,interface,Java,Generics,Interface,我有一个API,它返回实体的JSON列表,例如基因,蛋白质,资源,等等。例如,蛋白质端点返回: { next: "/api/1.0/protein?cursor=200", entities: [ { symbol: "TARSH_HUMAN", href: "/api/1.0/protein/TARSH_HUMAN" }, { symbol: "ABL1_HUMAN", href: "/ap

我有一个API,它返回实体的JSON列表,例如
基因
蛋白质
资源
,等等。例如,
蛋白质
端点返回:

{
    next: "/api/1.0/protein?cursor=200",
    entities: [
    {
        symbol: "TARSH_HUMAN",
        href: "/api/1.0/protein/TARSH_HUMAN"
    },
    {
        symbol: "ABL1_HUMAN",
        href: "/api/1.0/protein/ABL1_HUMAN"
    },
    ...
资源
端点完全相同,除了
符号
名称

迄今为止的解决方案

我想做的是创建某种类型的
EntityListSchema
,无论实体是什么,都可以生成这样的列表。以下是我所拥有的:

public class EntityListSchema<T extends JsonModel> {

    private String next;

    private List<T> entities;

    public EntityListSchema(Class<T> klass, int startAt) {
        int nextInt = startAt + Constant.API_MAX_RESULTS;
        this.next = "/" 
            + Constant.API_URL + "/"
            + klass.getEndpoint() + "?" // <-- This is the problem
            + Constant.API_CURSOR + "=" + nextInt;
    }

    ...
公共类EntityListSchema{
私有字符串下一步;
私人名单实体;
公共EntityListSchema(类klass,int startAt){
int nextInt=startAt+Constant.API\u MAX\u结果;
this.next=“/”
+Constant.API_URL+“/”

+klass.getEndpoint()+“?”//您混淆了对象实例及其类,例如,对于字符串,
string.class
是类(您的
class
),而
“foobar”
是实例(列表中的
T
元素),在本例中,仅
“foobar”
String
方法,
String.class
class
方法


我的建议是在这里使用一些注释来定义“端点”,然后使用您混淆了对象实例及其类,例如,对于字符串,
string。class
是类(您的
class
),而
“foobar”
是实例(列表中的
T
元素),在本例中,仅
“foobar”
String
方法,
String.class
class
方法


我的建议是在这里使用一些注释来定义“端点”,然后使用

您对类的
Java类
实例
存在混淆

类是定义。 该类的实例是实现

Class<T> klass

您混淆了类的
Java类
实例

类是定义。 该类的实例是实现

Class<T> klass
PS BTW支持将端点识别为单个单词,以便将其命名为getEndpoint,而不是getEndpoint


PS BTW支持将端点识别为单个单词,因此将其命名为getEndpoint,而不是getEndpoint。

类klass
并不代表类的实例,您只需更改:
t klass
您的问题实际上是,我们如何将每个类的元数据关联起来class@bayou.io,你可能是对的。我一定会听重新设计的建议如果这是您的建议,尽管从技术上讲可能无法回答问题。
Class
不代表类的实例,您只需更改:
t klass
您的问题实际上是,我们如何根据class@bayou.io,你可能是对的。我肯定会听重新设计的建议如果这是你的建议,尽管从技术上来说,它可能无法回答这个问题。
public EntityListSchema(Class<T> klass, int startAt) {
...
    + klass.getEndpoint() 
class JsonModel {
    abstract String getEndpoint();
    ...
}
...
public EntityListSchema(List<T> entities, int startAt) {
    this.entities = entities;
    if (!entities.empty()) {
        int nextInt = startAt + Constant.API_MAX_RESULTS;
        this.next = "/" 
            + Constant.API_URL + "/"
            + entities.get(0).getEndpoint() + "?" // <-- This is the problem
            + Constant.API_CURSOR + "=" + nextInt;
}