Object对其他类的类型转换
Object obj = new Minisun();
一。String
String str = String.valueOf(obj);
obj.toString()
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
二。Interger
Interger inter = Interger.parseInt(obj.toString());
三。Long
Long l = Long.valueOf(obj.toString());
四。BigDecimal
public static BigDecimal getBigDecimal( Object value ) {
BigDecimal ret = null;
if( value != null ) {
if( value instanceof BigDecimal ) {
ret = (BigDecimal) value;
} else if( value instanceof String ) {
ret = new BigDecimal( (String) value );
} else if( value instanceof BigInteger ) {
ret = new BigDecimal( (BigInteger) value );
} else if( value instanceof Number ) {
ret = new BigDecimal( ((Number)value).doubleValue() );
} else {
throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");
}
}
return ret;
}