我意识到接口(interface)遗留的抽象方法的声明是可选的,但我想知道包含它们在语义上是否被认为是更好的形式。
接口(interface):
public interface Interface {
//Other methods....
public void unimplementedMethod();
}
抽象类:
public abstract Class AbstractClass implements Interface {
//Implemented methods....
public abstract void unimplementedMethod(); //Should include or better to leave out?
}
最佳答案
您应该将它们排除在外。 它们不会增加任何好处,只会增加要维护的代码量。如果您想删除该方法,您需要在另一个位置执行此操作。
代码质量分析器通常要求减少任何不需要的东西:冗余修饰符(例如:接口(interface)中的public abstract
),声明的运行时异常(throws IllegalArgumentException
),冗余抛出(throws Exception, IOException
)等。
但是,您可以考虑在抽象类中实现方法的默认行为(具有空主体的事件监听器方法、返回默认值的配置属性方法,...)。 例如:
public boolean isPersistent() {
return true;
}
isPersistent()
只需要在使用不同于默认值的其他值的子类中被覆盖。
关于java - 抽象类包含来自接口(interface)的抽象方法是否被认为是更好的形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27046709/