gpt4 book ai didi

java - 编码为泛型类型

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:38 24 4
gpt4 key购买 nike

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

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

Producer 界面如下所示。

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

这个接口(interface)的实现是这样的

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

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

我真正需要在 produce 方法中做的是类似

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() 中,我需要检查客户端程序使用的泛型类型,并根据它更改函数的行为。这并不是说我想将泛型类型限制为特定类型的对象(我可以在其中使用通配符),而是我需要函数 produce() 的行为根据泛型中使用的对象类型略有不同客户端代码。

最佳答案

怎么样:

public class IntegerProducer implements Producer<Integer> {

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

因为您的 IntegerProducer 只处理 Integer,所以您不需要它是通用的。然后你可以写:

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();
}
}

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

关于java - 编码为泛型类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14155781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com