English 中文(简体)
通用实例不可行? 备选案文或建议?
原标题:Generics example does not work? Alternatives or suggestions?

我正在用以下法典犯这一错误:

<编码> 这种方法可得出结果(Capture#2-of?) 普通类 2. 基因试验:基础设计; 扩大基因试验。 项目编号:

import org.junit.Test;

public class GenericTest {

public class Item {
    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

public class WordItem extends Item {

}

public abstract class BaseContainer<T extends Item> {
    public String getItemProperty(T item) {
        return item.getValue();
    }
}

public class Document extends BaseContainer<WordItem> {
}

public static class ContainerFactory {
  public static BaseContainer<? extends Item> getContainer(Item item) {
     return new GenericTest().new Document();
  }
}

@Test
public void theTest(){
    Item item = new WordItem();
    BaseContainer<? extends Item> container = ContainerFactory.getContainer(item);
    String str = container.getItemProperty(item); //this line does not compile
}
}

I ́m利用Eclipse Helios, jdk 1.6.0_16。

最佳回答

为了消除错误,将你的法典改为:

public static class ContainerFactory {
  public static <T extends Item>BaseContainer<T> getContainer(Item item) {
     return (BaseContainer<T>) new GenericTest().new Document();
  }
}

@Test
public void theTest(){
    Item item = new WordItem();
    BaseContainer<Item> container = ContainerFactory.getContainer(item);
    String str = container.getItemProperty(item);
}
问题回答

我认为,问题在于:<代码>container 是BaseContainer<? 扩展项目和项目;,即为some <>/em>亚类<代码>。 然而,汇编者无法确保所涉次类实际上为<代码>Item本身。 事实上,它为<条码>WordItem,而不是<条码>。 总体问题是,你的通用参数使用不连贯和完整。

我可以将这些修改汇编成文:

public class Document<T extends Item> extends BaseContainer<T> {
}

public static class ContainerFactory {
  public static <T extends Item> BaseContainer<T> getContainer(T item) {
     return new Test().new Document<T>();
  }
}

@Test
public void theTest(){
    WordItem item = new WordItem();
    BaseContainer<WordItem> container = ContainerFactory.getContainer(item);
    String str = container.getItemProperty(item);
}

这与真正的软体建议不同,因为此处“文件也作了笼统的表述,这或许不是你想要的(你对此没有给出任何信息),但至少这样,在<条码>中就没有必要进行不安全的投放。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...