y割り当て行にタイプ変換の警告がある理由を誰もが説明できますか? xまたはzの割り当てには警告はありません。予期しないチェックされていない変換の警告
public class Entity<T>
{
@SuppressWarnings("unchecked")
public <TX> Entity<TX> typed(Class<TX> type)
{
return (Entity<TX>) this;
}
@SuppressWarnings("unchecked")
public static <TX> Entity<TX> typed(Entity<?> entity, Class<TX> type)
{
return (Entity<TX>) entity;
}
public static void main(final String[] args)
{
final Entity<?> a = new Entity<Integer>();
final Entity b = (Entity) a;
final Entity<Integer> x = a.typed(Integer.class);
final Entity<Integer> y = b.typed(Integer.class);
final Entity<Integer> z = typed(b, Integer.class);
}
}
あなたのエンティティ 'b'は、' a'からのキャストの後で型なしになるので、 'b.typed(Integer.class) 'で元に戻すと型なしになります。ジェネリック型はクラス型とは異なります。彼らは "ダウンキャスト"されても保持されません。 – Vulcan