次のシナリオを検討してください。
/**
* 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);
}
}
}
上記のコードで、Javaジェネリック実装は、MyService宣言が特定のタイプの特定のサブタイプについて話しているときに、MyServiceImplの実装がジェネリッククラスを返すことをどのように許可できますか?