Tag: unchecked cast

如何certificate为什么未经检查的强制转换是可以的,关于Copyable getObjectCopy()

(这是我上一个问题的后续行动。) 我有一个名为Copyable的接口,它有一个function Copyable getObjectCopy(); 这被许多其他类使用。 因为此函数始终返回Copyable ,所以会导致未经检查的强制转换。 例: @SuppressWarnings(“unchecked”) //Copy of itself is the same type. ValidateValue vvo = (ValidateValue)this_toCopy.getValidator().getObjectCopy(); vvBlkA = vvo; 我的问题与Josh Bloch的建议有关(在Effective Java,第2版,第24项): 每次使用@SuppressWarnings(“未选中”)注释时,请添加注释,说明为什么这样做是安全的。 他的榜样是 // This cast is correct because the array we’re creating // is of the same type as the one passed in, which is T[]. @SuppressWarnings(“unchecked”) T[] result […]

类型安全:未选中从Object转换为ArrayList

以下是将ArrayList从服务器发送到客户端的程序的一部分。 我想删除有关此代码中最后一行的警告: 客户代码: Socket s; (…) // A server is sending a list from the other side of the link. ois = new ObjectInputStream(s.getInputStream()); MyList = (ArrayList) ois.readObject(); MyVariable是一个具有一些属性的Java类。 服务器正在创建一个ArrayList并用MyVariable变量填充它作为项目。 然后它将完整列表发送给客户端。 我想知道为什么我会在那里发出警告以及如何完美编码以获得0警告。 如果有可能我想避免使用“@SuppressWarnings(”unchecked“)”。 ;) 谢谢, 路易斯