为什么有人要这样做?
private Number genericObjectToNumber (Object obj)
{
if (obj instanceof Byte)
{
return(new Byte((Byte) obj));
}
else if (obj instanceof Short)
{
return(new Short ((Short) obj));
}
......
else if(obj instanceof BigInteger)
{
return(BigInteger.ZERO.add ((BigInteger) obj));
}
return(null); // if it isn't a number, we don't want it
}
为什么不只返回 Actor 阵容?为什么要通过新对象的构造函数?为什么不问 obj 是否为实例
if (obj instanceof Number)
{
return((Number)obj);
}
最佳答案
我认为没有正当理由这样做。如果对象是可变的并且您想要创建这些对象的副本,那么这可能是有意义的。但原始包装类是不可变的,因此使用现有对象调用构造函数没有意义。
关于java - 为什么有人要这样做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28244719/