有什麼更好的方法,我可以比較Type
java通用其他硬編碼String
?比較Java泛型時如何避免字符串硬編碼?
在這種方法中,我想換硬編碼字符串的類的引用:
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
if (!type.toString().equals("com.package.app.LstMdl<com.package.app.AdMdl>")) { //I would like not to use this String
//do sth
}
return super.fromBody(body, type);
}
我試圖將其交換爲這樣:
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
if (!(type.equals(LstMdl.class) && ((LstMdl)type).data.getClass().equals(AdMdl.class))) {
//do sth
}
return super.fromBody(body, type);
}
,但它不工作。第一個解決方案工作正常,但有沒有辦法做到這一點更優雅?
不應該'type.equals(LstMdl.class)'是'型.getClass()。equals(LstMdl.class)' – FunkTheMonk
@FunkTheMonk無法正常工作:/ – fragon