我认为不能,因为标记接口(interface)原则是没有任何方法,但由于默认方法不是抽象我不确定。
最佳答案
就 Java 而言,“标记”接口(interface)只是一个常规接口(interface)。因此,它可以像任何 (Java-8) 接口(interface)一样具有默认方法。
现在,至于这是否违反了标记接口(interface)的原则,我不得不说是的。 Marker 接口(interface)应该充当某种标志,仅标识一个类满足某些外部条件。现在,它可以是一个标记接口(interface)并且具有抽象/默认方法,但它不再纯粹符合定义。
来自 Effective Java(第二版):
A marker interface is an interface that contains no method declarations, but merely designates (or “marks”) a class that implements the interface as having some property.
关于java - 可序列化的标记接口(interface)可以包含默认方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26723994/