How to cast List to List Object集合轉換成實體集合
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:

(List<Customer>)(Object)list; 
將List<Object>強制轉換成Object,然后再轉換為實體集合!
地址:https://stackoverflow.com/questions/1917844/how-to-cast-listobject-to-listmyclass

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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM