[英]Is it safe to typecast a “type-erased” object?
A class (implementing a generic interface) has a method which returns java.lang.Object
datatype instead of the expected. 类(实现通用接口)有一个方法,它返回
java.lang.Object
数据类型而不是期望的数据类型。 Is it safe to typecast it at the caller statement? 在调用者声明中对它进行类型转换是否安全?
import java.io.*;
import java.lang.*;
interface dataCharge<A>{
public A change(A ele);
}
class APCDcharge implements dataCharge<APCDcharge>{
private String name;
private Integer charge;
public void setName(String name){
this.name = name;
}
public void setCharge(Integer charge){
this.charge = charge;
}
public String getName(){
return name;
}
public Integer getCharge(){
return charge;
}
public String getKey(){
return name+":"+charge;
}
@Override
public APCDcharge change(APCDcharge ele){
APCDcharge newele = new APCDcharge();
newele.setCharge(ele.getCharge()+100);
newele.setName(ele.getName());
return newele;
}
}
public class Main {
public static void main(String[] args) {
APCDcharge c = new APCDcharge();
c.setName("ABC");
c.setCharge(100);
APCDcharge d = timePass(c);
System.out.println(d.getKey());
}
private static <T extends dataCharge> T timePass(T element){
T res = element.change(element);
return res;
}
}
The above code snippet gives error : 上面的代码段给出了错误:
error: incompatible types: Object cannot be converted to TT res = element.change(element);
I also tried typecasting element.change(element)
with T
datatype like : 我还尝试使用
T
数据类型进行类型转换element.change(element)
使用T
类型转换element.change(element)
T res = (T)element.change(element);
And the code is compiling (with a warning: Note: Main.java uses unchecked or unsafe operations.
), but executing correctly. 并且代码正在编译(带有警告:
Note: Main.java uses unchecked or unsafe operations.
),但正确执行。 Is that safe a way to remove the error? 这样可以安全地删除错误吗? Is there any other way to correct it?
有没有其他方法来纠正它?
Your problem is that in your static
method the type bound of T
is a raw type ( dataCharge
). 您的问题是在
static
方法中, T
的类型绑定是原始类型( dataCharge
)。
Make the following change: 进行以下更改:
private static <T extends dataCharge<T>> T timePass(T element){
T res = element.change(element);
return res;
}
ie T extends dataCharge<T>
instead of T extends dataCharge
. 即
T extends dataCharge<T>
而不是T extends dataCharge
。
Now the code will pass compilation without the cast. 现在代码将在没有强制转换的情况下通过编译。
This method: 这个方法:
static <T extends dataCharge> T timePass(T element){
T res = element.change(element);
return res;
}
Uses a raw (ie untyped) dataCharge
. 使用原始 (即无类型)
dataCharge
。 You should type it, plus return the type of dataCharge
, not the (sub)class of dataCharge
. 你应该输入它,再加上返回的类型
dataCharge
,而不是(子)类的dataCharge
。
Given way you have used it, which is to pass itself into its own method, suggests that dataCharge
should be more simply defined with as a supplier of dataCharge
and directly access its own state when creating the result: 给定你使用它的方式,即将自己传递给自己的方法,建议
dataCharge
应该更简单地定义为dataCharge
的供应商 ,并在创建结果时直接访问自己的状态:
interface dataCharge<A>{
public A change();
}
class APCDcharge implements dataCharge<APCDcharge> {
// other code
@Override
public APCDcharge change() {
APCDcharge newele = new APCDcharge();
newele.setCharge(getCharge()+100);
newele.setName(getName());
return newele;
}
}
Then your timePass
method then becomes: 然后你的
timePass
方法变为:
static <T> T timePass(dataCharge<T> element){
T res = element.change();
return res;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.