Java 具有反向关系的通用接口
我想创建两个具有反向关系的接口Java 具有反向关系的通用接口,java,generics,interface,Java,Generics,Interface,我想创建两个具有反向关系的接口 public interface Item <D extends Description, C extends Category<D,Item<D,C>>> { public C getCategory(); public void setCategory(C category);} 但这会导致错误绑定不匹配:类型类别不是类型项的绑
public interface Item <D extends Description,
C extends Category<D,Item<D,C>>> {
public C getCategory();
public void setCategory(C category);}
但这会导致错误
绑定不匹配:类型类别不是类型项的绑定参数的有效替代品。如何使用泛型正确地参数化接口类别
编译时不会出现警告:
interface Description {}
interface Item<D extends Description, C extends Category<D, Item<D, C>>> {
public C getCategory();
public void setCategory(C category);
}
public interface Category<D extends Description, I extends Item<D, ?>> {
public List<I> getItems();
public void setItems(List<I> items);
}
接口描述{}
接口项{
公共C getCategory();
公共类(C类);
}
公共接口类别{
公共列表getItems();
公共项目(列表项目);
}
我制作了我扩展了项目
。虽然没有警告,但这可能会给您带来其他问题-如果有,请告诉我,我会看看我能做些什么。这似乎有效:)。我不知道该如何解释(我通常会尽量避免做那样的事情),但这里是:
interface Description {}
interface Item<D extends Description, I extends Item<D, I, C>, C extends Category<D, C, I>>
{
public C getCategory();
public void setCategory(C category);
}
interface Category<D extends Description, C extends Category<D, C, I>, I extends Item<D, I, C>> {
public List<I> getItems();
public void setItems(List<I> items);
}
class DescriptionImpl implements Description {}
class CustomItem implements Item<DescriptionImpl, CustomItem, CustomCategory> {
public CustomCategory getCategory() {
return null;
}
public void setCategory(CustomCategory category) {
}
}
class CustomCategory implements Category<DescriptionImpl, CustomCategory, CustomItem> {
public List<CustomItem> getItems() {
return null; }
public void setItems(List<CustomItem> items) {
}
}
customItem.getCategory()返回的类别类型是CustomCategory,我认为这正是您真正想要的。花点时间阅读以下内容:让我们先忘掉描述,制作一些漂亮的对称接口:
interface Item<C extends Category<Item<C>>> {}
interface Category<I extends Item<Category<I>>> {}
接口项{}
接口类别{}
对于描述,您可能需要两种不同的项目和类别描述类型
总的来说,我不确定你的设计是不是一个好主意。我不知道你会用它做什么,但它看起来很难正确使用
另外,item.setCategory(c)
似乎有问题,因为item
已经被一个类别参数化了。考虑这个简化的问题
interface Item<C extends Container<Item<C>>>
interface Container<I extends Item<Container<I>>>
因为我们没有这个
,所以可以尝试通过类型参数来接近它
interface Item<C extends Container<This, C>>, This extends Item<C, This> >
interface Container<I extends Item<This, I>, This extends Container<I, This>>
再一次,我们必须依靠程序员的纪律来避免这样的事情。既然我知道我有什么样的项目,我不认为
是我想要使用的。是:)这对我来说更奇怪,因为我无法真正解释:)
interface Item<C extends Category<Item<C>>> {}
interface Category<I extends Item<Category<I>>> {}
interface Item<C extends Container<Item<C>>>
interface Container<I extends Item<Container<I>>>
interface Item<C extends Container<? extends Item<C>>>
interface Container<I extends Item<? extends Container<I>>>
class MyItem implements Item<MyContainer>
class MyContainer implements Container<MyItem>
class HerItem implements Item<MyContainer> // nooo!
interface Item<C extends Container<This>>>
interface Container<I extends Item<This>>
interface Item<C extends Container<This, C>>, This extends Item<C, This> >
interface Container<I extends Item<This, I>, This extends Container<I, This>>
interface Item<C extends Container<I, C>>, I extends Item<C, I> >
interface Container<I extends Item<C, I>, C extends Container<I, C>>
class HerItem implements Item<MyContainer, MyItem> // uh?