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

发布于 2024-10-15 04:43:51 字数 699 浏览 5 评论 0原文

我正在编写一个抽象类,并且有一个应该由子类定义的变量(在本例中它是一个在超类中使用的 int )。我无法决定是否定义一个具有默认值的受保护变量并让子类在构造函数中或通过 setter 方法更改值。或者在父类中定义一个抽象方法,使得所有子类都必须实现它并返回它们想要使用的值(然后使用抽象方法访问超类中的方法)。谁能告诉我是否有任何重要的理由说明为什么应该优先选择一种方式?

抽象方法:

public abstract int getValue();

public int getValue() {
    return 5;
}
  • 优点:

强制子类考虑这个值并选择他们想要的值。

  • 缺点:

没有默认值,因此即使大多数子类只想使用相同的值,他们仍然必须实现该方法。 (该方法可以设为非抽象并返回默认值,但是这样您就失去了强制子类考虑该值的优势)

受保护的变量:

protected int value;

public SubClassImpl() {
    value = 5;
}
  • 优点:

可以定义默认值,子类可以忽略它如果他们不在乎的话。

  • 缺点:

子类的作者不知道该变量的存在,因此他们可能会对默认行为感到惊讶。

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). 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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

起风了 2024-10-22 04:43:51

嗯,这取决于...

  • 该值是否必须能够在对象的生命周期内更改?如果不是,您可能希望将其设置为受保护的构造函数参数,并使其成为基类中的最终私有变量。

  • 子类是否有理由根据其他可变状态来计算变量?如果是这样,抽象 getter 将是合适的。

  • 否则,我可能会使用私有变量和受保护的设置器。我通常不喜欢非私有变量。这可以让您对基类中值的更改做出反应,例如更改其他计算机字段。为了避免出现默认值,您可以将其与构造函数参数结合起来,以强制子类稍后可以通过设置器更改初始值。

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. 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.

浅听莫相离 2024-10-22 04:43:51

我通常使用抽象方法,因为这允许您利用 协变返回类型,但(可变)字段或属性则不然。

例如,在 Guava 中,这允许 ForwardingSortedMap 扩展ForwardingMap

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文