[英]Should I use an abstract method or an instance variable for data that should be specified by sub-class?
I am writing an abstract class and I have a variable that should be defined by the sub-class (in this case it is an int which is used in the super-class). 我正在编写一个抽象类,我有一个应该由子类定义的变量(在这种情况下,它是一个在超类中使用的int)。 I can't decide whether to define a protected
variable with a default value and let the sub-class change the value either in the constructor or through a setter
method. 我无法决定是否使用默认值定义protected
变量,并让子类在构造函数中或通过setter
方法更改值。 Or to define an abstract method in the parent class such that all sub-classes must implement it and return the value they want to be used (then access the method in the super-class using the abstract method). 或者在父类中定义一个抽象方法,以便所有子类必须实现它并返回它们想要使用的值(然后使用抽象方法访问超类中的方法)。 Can anyone tell me if there are any great reasons why one way should be preferred over the other? 任何人都可以告诉我,为什么一种方式优先于另一种方式有任何重要原因?
Abstract Method: 抽象方法:
public abstract int getValue();
public int getValue() {
return 5;
}
Forces sub-class to think about this value and choose what they want it to be. 强迫子类考虑这个值并选择他们想要的值。
No default value, so even if most of the sub-classes just want to use the same value they still have to implement the method. 没有默认值,因此即使大多数子类只想使用相同的值,他们仍然必须实现该方法。 (The method can be made non-abstract and return the default value, but then you lose the advantage of forcing sub-classes to think about the value) (该方法可以是非抽象的并返回默认值,但是你失去了强迫子类思考值的优势)
Protected Variable: 受保护的变量:
protected int value;
public SubClassImpl() {
value = 5;
}
Can define a default value and sub-classes can just ignore it if they don't care. 可以定义默认值,如果子类不关心,子类可以忽略它。
Authors of the sub-class aren't made aware of the existence of the variable, so they might be surprised by the default behaviour. 子类的作者没有意识到变量的存在,因此他们可能会对默认行为感到惊讶。
Well, it depends... 这要看情况...
Does the value have to be able to change during the lifetime of the object? 值是否必须能够在对象的生命周期内更改? If not, you might want to make it a protected constructor parameter, and make it a final private variable in your base class. 如果没有,您可能希望将其作为受保护的构造函数参数,并使其成为基类中的最终私有变量。
Might subclasses have reason to compute the variable based on other mutable state? 可能的子类有理由根据其他可变状态计算变量吗? If so, an abstract getter would be appropriate. 如果是这样,抽象的吸气剂将是合适的。
Otherwise, I'd probably use a private variable and a protected setter. 否则,我可能会使用私有变量和受保护的setter。 I'm generally not a fan of non-private variables. 我一般不喜欢非私人变量。 This could allow you to react to changes in the value in your base class, such as changing other computer fields. 这可以允许您对基类中值的更改做出反应,例如更改其他计算机字段。 To avoid the presence of a default value you could combine this with a constructor parameter to force an initial value which the subclass could change later via the setter. 为了避免出现默认值,您可以将其与构造函数参数结合使用,以强制初始值,子类可以稍后通过setter进行更改。
I usually use an abstract method, because that allows you to take advantage of covariant return types but a (mutable) field or property does not. 我通常使用抽象方法,因为这允许您利用协变返回类型,但(可变)字段或属性不允许。
For example in Guava , this allows ForwardingSortedMap to extend ForwardingMap . 例如在Guava中 ,这允许ForwardingSortedMap扩展ForwardingMap 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.