为什么反射返回两个方法,而只有一个实现?
2022-09-03 15:42:45
假设我有这个代码:
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++;
}
}
变量将产生 2;为什么会这样?getAddressMethodCount