[英]How to cast an object to the class returned by getClass()
ProtostuffIOUtil.mergeFrom(data,o,RuntimeSchema.getSchema(o.getClass()));
How to cast Object to getClass() Class. 如何将Object转换为getClass()类。 With the above method call, I will get a compile error because the method requires that the o variable is of the same class as the o.getClass() parameter.
使用上面的方法调用,我将得到一个编译错误,因为该方法要求o变量与o.getClass()参数属于同一个类。 How does one get around this?
怎么绕过这个?
Class clazz = o.getClass();
ProtostuffIOUtil.mergeFrom(data, clazz.cast(o), RuntimeSchema.getSchema(clazz));
使用cast()方法从getClass()将对象强制转换为类:
myObj.getClass().cast(myObj)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.