private String getMainTitle(List<String> bs){
for(String b:bs){
if (b.equals("GOOD"){
return "string";
}
}
return null;
}
private String getMainTitle(String b){
if (b.equals("GOOD"){
return "string";
}
return null;
}
public String getTitleFromString(String b){
return getMainTitle(b);
}
public String getTitleFromList(List<String> bs){
return getMainTitle(bs);
}
請問怎样在JAVA寫一個含未知類的參數的方法?
希望寫一個方法能替代getTitleFromString和getTitleFromList , 比如:
public String getTitleFromList(T<?> t){
return getMainTitle(t);
}
但我的語法有誤, 請問該怎样改寫?
ringa_lee2017-04-18 10:53:05
这样可行
private <T> T getMainTitle(T t){
if (t.equals("GOOD")){
return (T)"string";
}
return null;
}
public <T> T getTitleFromString(T t){
return getMainTitle(t);
}