Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在方法接口中返回泛型类型_Java - Fatal编程技术网

Java 如何在方法接口中返回泛型类型

Java 如何在方法接口中返回泛型类型,java,Java,如何为接口定义泛型返回类型,使其实现的类可以有自己的返回类型 public interface A { public <T> T doSomething(); } public class ImplA implements A { public SomethingElseA doSomething() { return obj.doSomething(); } } public class ImplB implements A

如何为接口定义泛型返回类型,使其实现的类可以有自己的返回类型

public interface A {
    public <T> T doSomething();     
}


public class ImplA implements A {
    public SomethingElseA doSomething() {
        return obj.doSomething();
    }
}

public class ImplB implements A {
    public SomethingElseB doSomething() {
        return obj.doSomething();
    }
}

试着做如下的事情

interface A<T> {

  T doSomething();
}

class ImplA implements A<SomethingElseA> {

  public SomethingElseA doSomething() {
    ...
  }
}

class ImplB implements A<SomethingElseB> {

  public SomethingElseB doSomething() {
    ...
  }
}

试着做如下的事情

interface A<T> {

  T doSomething();
}

class ImplA implements A<SomethingElseA> {

  public SomethingElseA doSomething() {
    ...
  }
}

class ImplB implements A<SomethingElseB> {

  public SomethingElseB doSomething() {
    ...
  }
}

我猜你是说像这样?我把do改成foo,因为do是一个保留字

public interface A<T> {
    public T foo();      
}

public class ImplA implements A<SomethingElseA> {
    @Override
    public SomethingElseA foo() {
        return obj.doSomething();
    }
}

public class ImplB implements A<SomethingElseB> {
    @Override
    public SomethingElseB foo() {
        return obj.doSomething();
    }
}

我猜你是说像这样?我把do改成foo,因为do是一个保留字

public interface A<T> {
    public T foo();      
}

public class ImplA implements A<SomethingElseA> {
    @Override
    public SomethingElseA foo() {
        return obj.doSomething();
    }
}

public class ImplB implements A<SomethingElseB> {
    @Override
    public SomethingElseB foo() {
        return obj.doSomething();
    }
}

do不是有效的标识符。@MaVRoSCy ok在什么意义上?它仍然无法编译。@MaVRoSCy删除t周围的括号,并指定t是接口的类型参数。do不是有效的标识符。@MaVRoSCy确定在什么意义上?它仍然无法编译。@Mavrossy删除t周围的括号,并指定t是接口的类型参数。