问题
ProtostuffIOUtil.mergeFrom(data,o,RuntimeSchema.getSchema(o.getClass()));
How to cast Object to getClass() Class. 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. How does one get around this?
回答1:
Class clazz = o.getClass();
ProtostuffIOUtil.mergeFrom(data, clazz.cast(o), RuntimeSchema.getSchema(clazz));
回答2:
Cast an object to the class from getClass() using the cast() method:
myObj.getClass().cast(myObj)
来源:https://stackoverflow.com/questions/7869013/how-to-cast-an-object-to-the-class-returned-by-getclass