我对 Interface 和 BeanInfo Introspector 中的默认方法有一点问题。在这个例子中,有接口:接口
public static interface Interface {
default public String getLetter() {
return "A";
}
}
和两个类ClassA和ClassB:
public static class ClassA implements Interface {
}
public static class ClassB implements Interface {
public String getLetter() {
return "B";
}
}
在 main 方法中,app 从 BeanInfo 打印 PropertyDescriptors:
public static String formatData(PropertyDescriptor[] pds) {
return Arrays.asList(pds).stream()
.map((pd) -> pd.getName()).collect(Collectors.joining(", "));
}
public static void main(String[] args) {
try {
System.out.println(
formatData(Introspector.getBeanInfo(ClassA.class)
.getPropertyDescriptors()));
System.out.println(
formatData(Introspector.getBeanInfo(ClassB.class)
.getPropertyDescriptors()));
} catch (IntrospectionException e) {
e.printStackTrace();
}
}
结果是:
class
class, letter
为什么默认方法“字母”在 ClassA 中不作为属性可见?是错误还是功能?