1

I realize the declaration of abstract methods left over from an interface is optional, but I'm wondering whether or not it's considered better form semantically to include them.

Interface:

public interface Interface {
//Other methods....
public void unimplementedMethod();
}

Abstract class:

public abstract Class AbstractClass implements Interface {
//Implemented methods....
public abstract void unimplementedMethod();   //Should include or better to leave out?
}
4

1 回答 1

4

你应该把它们排除在外。 它们不会增加任何好处,只会增加要维护的代码量。如果要删除该方法,则需要在另一个位置执行此操作。

代码质量分析器通常要求减少任何不需要的东西:冗余修饰符(例如:public abstract在接口中)、声明的运行时异常(throws IllegalArgumentException)、冗余抛出(throws Exception, IOException)等等。

但是,您可以考虑在抽象类中实现方法的默认行为(具有空主体的事件侦听器方法、返回默认值的配置属性方法……)。例如:

public boolean isPersistent() {
  return true;
}

isPersistent()只需要在使用不同于默认值的其他值的子类中被覆盖。

于 2014-11-20T18:24:30.630 回答