【发布时间】:2015-01-31 07:57:56
【问题描述】:
为什么有人会这样做?
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
}
为什么不只返回演员表?为什么要通过新对象的构造函数?为什么不问是否 obj instanceof
if (obj instanceof Number)
{
return((Number)obj);
}
【问题讨论】:
-
没什么好理由的,只是有些人很傻。
-
在java中你如何区分一个对象是真的
byte还是真的long?有没有类似反射的东西? -
@ryanyuyu
bytes 和longs 不是对象。 -
只是猜测,但语法表明作者不是经验丰富的 Java 开发人员。尤其是把返回值用括号括起来,就好像 return 是一个方法调用一样,看起来非常非 Java 风格。
标签: java