最後,我想要做一些類似於這個的東西,我將使用它來搜索正確的構造函數進行反射。比較兩個類,並考慮它們是否相等考慮原始類和其包裝類
public static boolean equalWithPrimitive(Class<?> from, Class<?> target){
if(from == target){
return true;
}else if((from == Byte.class || from == byte.class) && (target == Byte.class || target == byte.class)){
return true;
}else if((from == Short.class || from == short.class) && (target == Short.class || target == short.class)){
return true;
}else if((from == Integer.class || from == int.class) && (target == Integer.class || target == int.class)){
return true;
}else if((from == Long.class || from == long.class) && (target == Long.class || target == long.class)){
return true;
}else if((from == Float.class || from == float.class) && (target == Float.class || target == float.class)){
return true;
}else if((from == Double.class || from == double.class) && (target == Double.class || target == double.class)){
return true;
}else if((from == Boolean.class || from == boolean.class) && (target == Boolean.class || target == boolean.class)){
return true;
}else if((from == Character.class || from == char.class) && (target == Character.class || target == char.class)){
return true;
}
return false;
}
有沒有一個簡短而準確的方法來實現這個想法?
? – TheLostMind
你可以把這些類放在數組中,並循環它們。 – Justin
@TheLostMind是的,我希望它是真實的,如果兩個類是字節和字節或字節和字節或字節和字節或字節和字節 – user3213904