Nemo MVC中用到的,先写一下。
/**
* 类型转换
* @param value 需要转换的值
* @param type 需要转换的类型
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T cast(Object value, Class<T> type) {
if (value != null && !type.isAssignableFrom(value.getClass())) {
if (is(type, int.class, Integer.class)) {
value = Integer.parseInt(String.valueOf(value));
} else if (is(type, long.class, Long.class)) {
value = Long.parseLong(String.valueOf(value));
} else if (is(type, float.class, Float.class)) {
value = Float.parseFloat(String.valueOf(value));
} else if (is(type, double.class, Double.class)) {
value = Double.parseDouble(String.valueOf(value));
} else if (is(type, boolean.class, Boolean.class)) {
value = Boolean.parseBoolean(String.valueOf(value));
} else if (is(type, String.class)) {
value = String.valueOf(value);
}
}
return (T) value;
}