Q.如果你在一个没有链接到接口的抽象类中创建抽象方法,你是否仍然遵循程序到接口的原则?
我已经为我创建的所有 UI 类使用了一个接口;但是,考虑到接口的原因,我看不到与我要创建的抽象方法和已经存在的接口的直接关联。
通常,我会创建抽象方法并完成;但是,我想知道我是否违反了Program to an Interface的设计原则。
问:我应该为此创建另一个接口还是只使用抽象方法?
注意:这不是接口与抽象类的问题。
public abstract class BaseClass extends Clazz implements Interface {
// 1 out of 4 interface methods are implemented here
CustomObj getMode() { ... }
// I am actually also thinking of taking that 1 method - getMode() - out of the
// interface and implementing it as an implemented method without an interface.
// Method that made me ask this question!!
abstract CustomObj getDefaultMode(); // Not related to the interface - Interface
}
public interface Interface {
String getFirstNumber();
String getSecondNumber();
CustomObj getMode(); // This one is implemented in the BaseClass, but I think it no longer belongs in this role
// CustomObj getDefaultMode(); // This one is not in Interface, but makes be believe I need a new Interface or just have them declared in an Abstract Class.
}
注意:我的基类更多是为了简化具体类中的代码。基类处理一些被覆盖的方法、辅助方法、初始化等……所以它不像一个接口,而是你的标准抽象类。