【问题标题】:Is there a standard library with a generic reference interface?是否有具有通用参考接口的标准库?
【发布时间】:2012-02-29 23:46:30
【问题描述】:

我使用通用接口来定义给定类型T 的引用。目的是在方法参数中通过引用传递类型。

interface Reference<T> {
   void set(T t);
   T get();
}

我迅速查看并没有在 Google Guava 中看到类似的内容。有谁知道定义这个 interface 的库(可能连同实现一起)?我担心会找到如下所示的接口不是实现。AtomicReference 是实现,不提供接口。

另外请注意,我不是在寻找为类成员生成 getter/setter 方法的工具。这一点是通过引用传递方法参数。

我专门寻找接口的原因是因为我想在提供额外行为的额外类中实现接口。例如,我可以通过添加一个save() 方法来创建一个扩展ReferencePersistentReference

如果我发现一个比我的程序更广泛的已发布接口,那么假设的PersistentReference 可以在需要Reference 方法参数的类中使用。

【问题讨论】:

  • 更好地解释你想要什么。

标签: java generics reference pass-by-reference


【解决方案1】:

第一个想法:使用 Guice Provider interface 并将其扩展到您的需要。

【讨论】:

    【解决方案2】:

    java.util.concurrent.Future&lt;T&gt; 似乎最接近。

    【讨论】:

    • 怎么样?我认为我没有看到这种关系。
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 2015-09-19
    • 2011-07-10
    • 2010-10-15
    • 1970-01-01
    相关资源
    最近更新 更多