简体   繁体   English

编译生成以下错误:“错误:不兼容的类型:推理变量 T 具有不兼容的等式约束 Todo,TodoCouchDB”

[英]Compiling generating the following error: “error: incompatible types: inference variable T has incompatible equality constraints Todo,TodoCouchDB”

I'm trying to learn Java by writing a little todo list console app that can save todos to either the filesystem or to CouchDB.我正在尝试通过编写一个可以将待办事项保存到文件系统或 CouchDB 的小待办事项列表控制台应用程序来学习 Java。 Unfortunately, I'm hitting a compiler issue reading todos from CouchDB (using the LightCouch library) and have absolutely no idea how to move forward.不幸的是,我遇到了从 CouchDB 读取 todos 的编译器问题(使用LightCouch库),并且完全不知道如何继续前进。

I have the following classes:我有以下课程:

  • Todo , TodoFilesystem , TodoCouchDB TodoTodoFilesystemTodoCouchDB
  • Listx , ListFilesystem , ListCouchDB Listx , ListFilesystem , ListCouchDB

Here's the content of ListCouchDB :这是ListCouchDB的内容:

public class ListCouchDB extends Listx {
    public void load() {
        CouchDbClient dbClient = new CouchDbClient();
        list = dbClient.view("_all_docs").includeDocs(true).query(TodoCouchDB.class);
    }
}

The list variable is an instance variable defined on the superclass: list变量是定义在超类上的实例变量:

public class Listx {
    public List<Todo> list = new ArrayList<Todo>();

    ...
}

Compiling this code throws the following compiler error:编译此代码会引发以下编译器错误:

ListCouchDB.java:12: error: incompatible types: inference variable T has incompatible equality constraints Todo,TodoCouchDB
        list = dbClient.view("_all_docs").includeDocs(true).query(TodoCouchDB.class);
                                                                 ^
  where T is a type-variable:
    T extends Object declared in method <T>query(Class<T>)
1 error

When I change query(TodoCouchDB.class) to query(Todo.class) in the ListCouchDB class it actually compiles and correctly populates my list;当我在ListCouchDB class 中将query(TodoCouchDB.class)更改为query(Todo.class)时,它实际上会编译并正确填充我的列表; however, the list contains an array of Todo items and therefore if I try to call any methods on an item in the list it incorrectly calls the methods on the Todo superclass and not the TodoCouchDB subclass.但是,该列表包含一个Todo项目数组,因此如果我尝试调用列表中某个项目的任何方法,它会错误地调用Todo超类而不是TodoCouchDB子类的方法。

Apologies if there is insufficient info.如果信息不足,请见谅。 here, but appreciate any input anyone is able to offer.在这里,但感谢任何人能够提供的任何意见。

You may try to use generic types.您可以尝试使用泛型类型。

Define your Listx using generics使用 generics 定义您的 Listx

public class Listx<T extends Todo> {
 public List<T> list = new ArrayList<T>();
 ...
}

Define ListCouchDB as an extension of Listx<TodoCouchDB>将 ListCouchDB 定义为Listx<TodoCouchDB>的扩展

public class ListCouchDB extends Listx<TodoCouchDB> {
    public void load() {
        CouchDbClient dbClient = new CouchDbClient();
        list = dbClient.view("_all_docs").includeDocs(true).query(TodoCouchDB.class);
    }
}

Now, your list property is a list of TodoCouchDB instances现在,您的list属性是 TodoCouchDB 实例的列表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java:不兼容类型:推理变量T具有不兼容的边界等式约束:下限:java.util.List &lt;&gt; - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> 不兼容的类型:推理变量T具有不兼容的边界等式约束:捕获#1的? 扩展java.lang.Object - incompatible types: inference variable T has incompatible bounds equality constraints: capture#1 of ? extends java.lang.Object java:不兼容的类型:推理变量 T 具有不兼容的边界等式约束:下限:java.util.List&lt;&gt; 用于添加字段 - java: incompatible types: inference variable T has incompatible bounds equality constraints: lower bounds: java.util.List<> for adding field 错误:不兼容的类型:推断变量R具有不兼容的范围 - error: incompatible types: inference variable R has incompatible bounds 推断变量T具有不兼容的范围错误 - inference variable T has incompatible bounds Error Java “错误:不兼容的类型:推理变量 E 的边界不兼容”错误 - Java "error: incompatible types: inference variable E has incompatible bounds" error 错误:不兼容的类型:推断变量R具有不兼容的界限(Lambda Java 8) - error: incompatible types: inference variable R has incompatible bounds (Lambda java 8) “错误:不兼容的类型:推理变量R具有不兼容的边界”当flatMap流在单行中时 - “error: incompatible types: inference variable R has incompatible bounds” when flatMap the stream in single line 获取编译时错误推理变量 T 的边界不兼容 - Getting compile time error Inference variable T has incompatible bounds java EnumSet,不兼容的类型:推断变量E具有不兼容的范围 - java EnumSet, incompatible types: inference variable E has incompatible bounds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM