考虑以下场景:
/**
* A sample interface.
*/
public interface MyInterface
{
}
/**
* First sample implementation of the above interface.
*/
public class MyClass1 implements MyInterface
{
public String toString()
{
return "[ My Class 1 ]";
}
}
/**
* Second sample implementation of the above interface.
*/
public class MyClass2 implements MyInterface
{
public String toString()
{
return "[ My Class 2 ]";
}
}
import java.util.Collection;
/**
* A service interface that declares a generic method
* returning a collection of subtype the interface defined above.
*/
public interface MyService
{
public <T> extends MyInterface<Collection<T>> myMethod();
}
import java.util.Arrays;
import java.util.Collection;
/**
* The implementation of the service interface
* that returns the generic type.
*/
public class MyServiceImpl implements MyService
{
@Override
public Collection<MyInterface> myMethod()
{
return Arrays.asList(new MyClass1(), new MyClass2());
}
}
import java.util.Collection;
/**
* Simple main class to drive the point
* I would like raise in the query below.
*/
public class MyMain
{
public static void main(String[] args)
{
MyService service = new MyServiceImpl();
Collection<MyClass1> list = service.myMethod();
// This works at runtime.
System.out.println(list);
for (MyClass1 obj : list)
{
// This throws ClassCastException at runtime.
System.out.println(obj);
}
}
}
在上面的代码中,当 MyService 声明谈论给定类型的特定子类型时,Java 泛型实现如何允许 MyServiceImpl 的实现返回泛型类?