您的位置:首页 > 移动开发 > Objective-C

How to cast List<Object> to List<MyClass> Object集合转换成实体集合

2017-09-01 14:28 726 查看
List<Object> list = getList();
return (List<Customer>) list;


Compiler says: cannot cast 
List<Object>
 to 
List<Customer>

不能将Object集合强制转换成实体集合!
 

you can always cast any object to any type by up-casting it to Object first. in your case:
[b][b]([b]List[/b]<[b]Customer[/b]>)([b]Object[/b])[b]list[/b][/b][b];[/b]
将List<Object>强制转换成Object,然后再转换为实体集合!
地址:https://stackoverflow.com/questions/1917844/how-to-cast-listobject-to-listmyclass[/b]
[/code]
you must be sure that at runtime the list contains nothing but Customer objects.
Critics say that such casting indicates something wrong with your code; you should be able to tweak your type declarations to avoid it. But Java generics is too complicated, and it is not perfect. Sometimes you just don't know if there is a pretty solution to satisfy the compiler, even though you know very well the runtime types and you know what you are trying to do is safe. In that case, just do the crude casting as needed, so you can leave work for home.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: