Java - 包含变量的抽象类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/236211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:51:08  来源:igfitidea点击:

Java - Abstract class to contain variables?

javaclassvariablesabstract

提问by Adam Asham

Is it good practice to let abstract classes define instance variables?

让抽象类定义实例变量是一种好习惯吗?

public abstract class ExternalScript extends Script {

    String source;

    public abstract void setSource(String file);

    public abstract String getSource();
}

The sub class, ExternalJavaScript.class, would then automatically get the source variable but I feel it's easier to read the code if all the sub classes themselves define the source, instead of from inheritance.

然后子类 ExternalJavaScript.class 会自动获取源变量,但我觉得如果所有子类本身定义源而不是继承,那么阅读代码会更容易。

What is your advice?

你的建议是什么?

/Adam

/亚当

采纳答案by Egwor

I would have thought that something like this would be much better, since you're adding a variable, so why not restrict access and make it cleaner? Your getter/setters should do what they say on the tin.

我本以为这样的事情会好得多,因为您要添加一个变量,那么为什么不限制访问并使其更干净呢?你的 getter/setter 应该按照他们在罐头上说的去做。

public abstract class ExternalScript extends Script {

    private String source;

    public void setSource(String file) {
        source = file;
    }

    public String getSource() {
        return source;
    }
}

Bringing this back to the question, do you ever bother looking at where the getter/setter code is when reading it? If they all do getting and setting then you don't need to worry about what the function 'does' when reading the code. There are a few other reasons to think about too:

回到问题上来,您是否在阅读 getter/setter 代码时费心查看它的位置?如果它们都进行了获取和设置,那么您在阅读代码时就无需担心函数“做什么”。还有一些其他原因需要考虑:

  • If source was protected (so accessible by subclasses) then code gets messy: who's changing the variables? When it's an object it then becomes hard when you need to refactor, whereas a method tends to make this step easier.
  • If your getter/setter methods aren't getting and setting, then describe them as something else.
  • 如果源受到保护(子类可以访问),那么代码就会变得混乱:谁在更改变量?当它是一个对象时,当您需要重构时就会变得困难,而方法往往会使这一步更容易。
  • 如果您的 getter/setter 方法没有获取和设置,那么将它们描述为其他东西。

Always think whether your class is really a different thing or not, and that should help decide whether you need anything more.

始终考虑您的课程是否真的与众不同,这应该有助于确定您是否还需要更多东西。

回答by Gishu

Sure.. Why not?
Abstract base classes are just a convenience to house behavior and data common to 2 or more classes in a single place for efficiency of storage and maintenance. Its an implementation detail.
Take care however that you are not using an abstract base class where you should be using an interface. Refer to Interface vs Base class

当然.. 为什么不呢?
抽象基类只是为了方便存储和维护效率而将 2 个或多个类共有的行为和数据放在一个地方。它是一个实现细节。
但是请注意,您没有在应该使用接口的地方使用抽象基类。请参阅接口与基类

回答by Egwor

Of course. The whole idea of abstract classes is that they can contain some behaviour or data which you require all sub-classes to contain. Think of the simple example of WheeledVehicle - it should have a numWheels member variable. You want all sub classes to have this variable. Remember that abstract classes are a very useful feature when developing APIs, as they can ensure that people who extend your API won't break it.

当然。抽象类的整个想法是它们可以包含一些您需要所有子类都包含的行为或数据。想想 WheeledVehicle 的简单示例——它应该有一个 numWheels 成员变量。您希望所有子类都具有此变量。请记住,抽象类在开发 API 时是一个非常有用的特性,因为它们可以确保扩展 API 的人不会破坏它。