假设我有这个代码:
public interface Address {
public int getNo();
}
public interface User<T extends Address> {
public String getUsername();
public T getAddress();
}
public class AddressImpl implements Address {
private int no;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
}
public class UserImpl implements User<AddressImpl> {
private String username;
private AddressImpl addressImpl;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public AddressImpl getAddress() {
return addressImpl;
}
public void setAddress(AddressImpl addressImpl) {
this.addressImpl = addressImpl;
}
}
运行代码:
int getAddressMethodCount = 0;
for (Method method : UserImpl.class.getMethods()) {
if (method.getName().startsWith("getAddress")) {
getAddressMethodCount++;
}
}
将产生 2getAddressMethodCount
变量;为什么会这样?