问:如果要在未链接到接口的抽象类中创建抽象方法,您是否仍遵循Program to the Interface的原则?
我已经为我创建的所有UI类使用了一个接口;但是,鉴于接口的原因,我没有看到与我想要创建的抽象方法和现有接口的直接关联.
通常,我只是创建抽象方法并完成;但是,我想知道我是否违反了程序到接口的设计原则.
问:我应该为此创建另一个接口还是坚持使用抽象方法?
注意:这不是接口与抽象类问题.
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.
}
注意:My Base类更多用于简化具体类中的代码.基类处理一些重写的方法,辅助方法,初始化等…所以它不像接口那样,而是作为标准的抽象类.