小编Omn*_*est的帖子

Java - 当返回类型为自己的方法参数类型使用泛型时,覆盖扩展接口的返回类型

我偶然发现了java继承的好奇心,我希望你能提出更好的想法:

假设有两个接口A和A1

接口A1扩展A.

接口A有一个返回泛型类型的方法.

通用类型就像GenericType<T>.

现在,一个基本思想是将此通用返回类型从GenericType<Object>接口A 更改 为 GenericType<String>接口A1

好吧一开始似乎很容易(后来会出现不好的事情)

我们声明接口A之类的

public interface InterfaceA {
  public GenericType<? extends Object> getAGenericType();  
}
Run Code Online (Sandbox Code Playgroud)

和接口A1一样

public interface InterfaceA1 extends InterfaceA
{
  @Override
  public GenericType<String> getAGenericType();
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我们被迫GenericType<? extends Object>在接口A本身中编写,以允许使用基于泛型的"子类"覆盖它.(实际上,generictype的泛型参数是子类,而不是泛型类型本身)

现在假设GenericType有自己的方法,如下所示:

public interface GenericType<D>
{
  public void doSomethingWith( D something );
}
Run Code Online (Sandbox Code Playgroud)

现在尝试实例化A1效果很好.

而是试图实例化A会很糟糕.要看看为什么要看这个"使用界面"类:

public class LookAtTheInstance
{
  @SuppressWarnings("null")
  public static void method()
  {
    InterfaceA a = null;
    InterfaceA1 a1 = null;

    GenericType<String> aGenericType = a1.getAGenericType();

    GenericType<? …
Run Code Online (Sandbox Code Playgroud)

java generics methods types return

13
推荐指数
1
解决办法
2万
查看次数

有谁知道像 Apache CXF 为 REST 提供的 Jersey 客户端代理实现

Apache CXF 项目为 REST 服务提供了一个基于代理的客户端实现。这看起来像:

Resource resource = JAXRSClientFactory.create( baseAddress, Resource.class )
Run Code Online (Sandbox Code Playgroud)

有人知道 Jersey 的类似实现吗?

我发现了一种使用@HyperMediaController注释的方法,但我想坚持使用 JSR-311 默认注释,例如@Path@Get...

有人有想法吗?

java rest cxf jersey

6
推荐指数
2
解决办法
2153
查看次数

标签 统计

java ×2

cxf ×1

generics ×1

jersey ×1

methods ×1

rest ×1

return ×1

types ×1