简体   繁体   English

我应该对应该由子类指定的数据使用抽象方法还是实例变量?

[英]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;
}
  • Pros: 优点:

Forces sub-class to think about this value and choose what they want it to be. 强迫子类考虑这个值并选择他们想要的值。

  • Cons: 缺点:

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;
}
  • Pros: 优点:

Can define a default value and sub-classes can just ignore it if they don't care. 可以定义默认值,如果子类不关心,子类可以忽略它。

  • Cons: 缺点:

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.

相关问题 为什么子类Java中相同方法的返回类型应该相同? - Why return type of same-named-method should be same in sub-class Java? 当子类的实例不可见时,为什么要在该子类的实例上调用私有方法? - Why can I invoke a private method on an instance of the sub-class when it shouldn't be visible to the instance? 当在子类的初始化块中重新初始化时,实例变量被覆盖,就像实例方法一样,它不应该被覆盖 - Instance variable, when re-initialised in sub class' initialization block, is overridden like instance method, which it should not 我应该使用哪种方法从文本中提取指定的数据? - Which method should I use to extract the specified data from a text? 我应该使用实例变量还是作为参数传递给方法 - Should I use Instance variable or pass as a parameter to method 很少使用抽象方法的抽象类-我应该具体化吗? - Abstract class with little used abstract method - should I make concrete? 如何确保从子类(Java)中的方法在抽象超类中调用某些方法 - How to ensure a certain methods gets called in abstract super-class from method in sub-class (Java) 如何在Java子类中调用该方法? - How do I call the method in the sub-class in java? 我应该使用额外的类还是为实例设置样式? - Should I use an extra class or style the instance? Hibernate-对于不是抽象的类,我应该使用哪种继承策略,但是子类可能不需要表 - Hibernate - Which inheritance strategy should I use for a class which isn't abstract, but sub-classes may not need tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM