3

我对 Java 泛型有一点奇怪的问题。实际上,这可能根本不是问题,可能只是我试图在设计中错误地使用泛型。我不会给出应用程序的全部细节(这是无关紧要和无聊的)。

我创建了一个小片段来捕捉问题的本质。

有一个 Producer 界面,看起来像这样。

public interface Producer<P> {
public P produce(); 
}

这个接口有一个实现,看起来像这样

public class IntegerProducer<P> implements Producer<P> {

@Override
public P produce() {
    return (P)new Integer(10);
}
}

我在生产方法中真正需要做的是

if (P instanceOf Integer) return (P)new Integer(10);
else throw new Exception("Something weird happened in the client app"); 

现在当然 P instanceOf Integer 将不起作用。但是,如果您了解我正在尝试做的事情的精髓,请您分享一个解决方案。

感谢您的帮助。

澄清1:问题不在于我只想返回整数。问题是在函数produce() 中,我需要检查客户端程序使用的泛型类型,并据此更改函数的行为。这并不是说我想将泛型类型限制为特定类型的 Object(我可以在其中使用通配符),而是我需要函数 generate() 根据泛型中使用的 Object 类型略有不同客户端代码。

4

1 回答 1

10

怎么样:

public class IntegerProducer implements Producer<Integer> {

    @Override
    public Integer produce() {
        return 10;
    }
}

由于您仅IntegerProducer处理Integers,因此您不需要它是通用的。然后你可以写:

Producer<Integer> p = new IntegerProducer();
Integer i = p.produce();

编辑

根据您的评论,我认为唯一的方法是传递一个类参数:

public class Producer<T> {

    public static void main(String[] args) throws InterruptedException {
        Producer<Integer> t1 = new Producer<> ();
        Producer<String> t2 = new Producer<> ();
        System.out.println(t1.get(Integer.class)); //prints 10
        System.out.println(t1.get(String.class)); //does not compile
        System.out.println(t2.get(String.class)); //throws exception
    }

    public T get(Class<T> c) {
        if (c == Integer.class) {
            return (T) (Object) 10;
        }
        throw new UnsupportedOperationException();
    }
}

或者,您可以在构造函数中传递它(例如 EnumSets 和 EnumMaps 使用的策略):

public class Producer<T> {

    public static void main(String[] args) throws InterruptedException {
        Producer<Integer> t1 = new Producer<> (Integer.class);
        Producer<Integer> t1 = new Producer<> (String.class); //does not compile
        Producer<String> t2 = new Producer<> (String.class);
        System.out.println(t1.get()); //prints 10
        System.out.println(t2.get()); //throws exception
    }

    private final Class<T> c;

    public Producer(Class<T> c) {
        this.c = c;
    }

    public T get() {
        if (c == Integer.class) {
            return (T) (Object) 10;
        }
        throw new UnsupportedOperationException();
    }
}

这显然使代码混乱,我个人会重新考虑设计以避免这种情况。

于 2013-01-04T10:56:32.060 回答