我无法在element和上调用字符串方法argument。UML图告诉我StartsWith该类具有泛型类型T>String。我在另一篇文章中读到,您需要以这种方式实现它<T extends String>。我想在这里的方法中用Object代替T,public boolean predicate(Object element, Object argument)但是编译器向我抛出那是不可能的。界面:public interface Intaf<T> { public boolean pres(T element, T argument);}班级:public class StartsWith <T extends String> implements Intaf { @Override public boolean pres(Object element, Object argument) { String firstLetterElement = element.substring(0,1); String firstLetterArgument = argument.substring(0,1); return firstLetterElement.equals(firstLetterArgument); }}
3 回答
慕盖茨4494581
TA贡献1850条经验 获得超11个赞
改成
public class StartsWith <T extends String> implements Intaf<T> {
@Override
public boolean pres(T element, T argument) {
String firstLetterElement = element.substring(0,1);
String firstLetterArgument = argument.substring(0,1);
return firstLetterElement.equals(firstLetterArgument);
}
}
慕桂英3389331
TA贡献2036条经验 获得超8个赞
您需要将asIntaf<T>更改并将参数更改为T类型
@Override
public boolean pres(T element, T argument) {
....
}
添加回答
举报
0/150
提交
取消