简体   繁体   English

Java Generics构造函数的意外行为 <X,Y> (C <Y> )和接口C. <Y>

[英]Java Generics Unexpected Behaviour for Constructor <X,Y> (C<Y>) and Interface C<Y>

I have the following code: 我有以下代码:

A class that models the mapping between a ValueContainer<P> and an Entity E . 一个模拟ValueContainer<P>和实体E之间映射的类。 (Eg. a checkbox (ValueContainer) and something that has a Boolean value): (例如,一个复选框(ValueContainer)和一个具有布尔值的东西):

public abstract class ObjValContainerMapper<E, P> {

    private ValueContainer<P> provider;

    public ObjValContainerMapper(ValueContainer<P> provider) {
        this.provider = provider;
    }

    public abstract P getValue(E entity);

    public abstract void setValue(E entity, P value);

    ...

}

An interface ValueContainer<P> : 接口ValueContainer<P>

public interface ValueContainer<T> {
        T getValue();
        void setValue(T value);
}

A custom Checkbox: 自定义复选框:

public class AdvancedCheckBox extends JCheckBox implements ValueContainer<Boolean>

And some code that unexpectedly doesn't work: 并且一些代码意外地不起作用:

AdvancedCheckBox chckbxBindToDrive = new AdvancedCheckBox(
            "Bind to Drive");

ObjValContainerMapper<IndexSpec, Boolean> bindToDriveMapper = 
        new ObjValContainerMapper<IndexSpec, Boolean>(chckbxBindToDrive) {
    @Override
    public Boolean getValue(IndexSpec entity) {
        if (entity == null) {
            return false;
        }
        return entity.isBindToDrive();
    }
    @Override
    public void setValue(IndexSpec entity, Boolean value) {
        entity.setBindToDrive(value);
    }
};

The code does not compile. 代码无法编译。 The error shown is " The constructor ObjValContainerMapper(AdvancedCheckBox) is undefined ". 显示的错误是“ 构造函数ObjValContainerMapper(AdvancedCheckBox)未定义 ”。 Eclipse suggests, among other options, to let AdvancedCheckBox implement ValueContainer or cast the argument chckbxBindToDrive to ValueContainer, despite AdvancedCheckBox explicitly declares implements ValueContainer<Boolean> . 除了AdvancedCheckBox显式声明implements ValueContainer<Boolean>之外,Eclipse还建议让AdvancedCheckBox实现ValueContainer或将参数chckbxBindToDrive转换为ValueContainer。 Strangely enough I have reused code from an old project that was built with Java 6, in which case the approx. 奇怪的是,我重用了使用Java 6构建的旧项目中的代码,在这种情况下大约是。 same code worked fine. 相同的代码工作正常。 Has something changed with Java 7 or am I missing something? Java 7有什么变化,或者我错过了什么?

Environment: 环境:

  • Eclipse Kepler Eclipse Kepler
  • JDK 1.7.0_51 JDK 1.7.0_51
  • some code generated with WindowBuilder (the AdvacedCheckBox declaration) 使用WindowBuilder生成的一些代码(AdvacedCheckBox声明)

Thx for all the troubleshooting proposals in the comments. Thx用于评论中的所有故障排除提议。

The cause for this behaviour was that I copied the ValueContainer interface twice into two differnt packages as I assumed it would be missing given that Eclipse didn't seem to find it first (thx @sasankad for the proposal to clean the project). 这种行为的原因是我将ValueContainer接口两次复制到两个不同的包中,因为我认为它不会首先找到它(因为清理项目的提议是@sasankad)。 I hope this is still helpful for others as the error message and solution-proposals by the compiler and Eclipse respectively are not and are rather misleading. 我希望这对其他人仍然有用,因为编译器和Eclipse的错误消息和解决方案提议分别不是并且相当误导。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM