【发布时间】:2012-12-27 16:29:56
【问题描述】:
我知道这是可能的,但有什么用?因为它只能投射与被投射对象相同的类型?
class Example<X>{
//statements
}
//then this is declared
Example<Integer> x = new Example<Integer>();
//This is allowed
(Example<Integer>) x;
//this is not allowed, so what's the use?
(Example<Long>) x;
【问题讨论】:
-
它不能编译,所以没有人可以使用它。我不明白你的问题。
-
你可以做
(Example<Long>)(Example) x。但这是愚蠢且容易出错的。你为什么要投给Example<Long>? -
@NikitaBeloglazov 好吧,我的书说它是可能的,但它没有给出具体的用途..所以我虽然我错过了一点
-
@vincentbelkin 你能在这里发布书中的确切报价吗?
-
所以在泛型中你可以转换“左”部分(实际类)而不是“右”部分(类型类)。
标签: java generics types casting