Java:接口与抽象类(关于字段)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/462198/
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-10-29 12:30:13  来源:igfitidea点击:

Java: Interface vs Abstract Class (regarding fields)

javainterfaceaccessibilityabstract-classfield

提问by Chris

From what I have gathered, I want to force a class to use particular private fields (and methods) I need an abstract class because an interface only declares public/static/final fields and methods. Correct??

从我收集到的信息来看,我想强制一个类使用特定的私有字段(和方法)我需要一个抽象类,因为接口只声明 public/static/final 字段和方法。正确的??

I just started my first big java project and want to make sure I'm not going to hurt myself later :)

我刚刚开始了我的第一个大型 Java 项目,并想确保以后不会伤害自己 :)

回答by Bombe

You don't want to force the use of certain private fields or methods. In general you don't care about the implementation, you care about the interface. So define a couple of methods in a couple of interfaces (depending on how much you need) and define classes that implement them. This will probably hurt you the least in the future.

您不想强制使用某些私有字段或方法。一般来说,你不关心实现,你关心接口。所以在几个接口中定义几个方法(取决于你需要多少)并定义实现它们的类。这可能在未来对你的伤害最小。

回答by Richard Campbell

It's fairly common to provide both, so that you end up with:

提供两者是相当常见的,因此您最终会得到:

public interface Sendable {
    public void sendMe();
}

and

public abstract class AbstractSender implements Sendable {
    public abstract void send();

    public void sendMe() {
        send(this.toString());
    }
}

That way, anyone who is happy with the default implementation in the abstract class can quickly subclass it without rewriting a lot of code, but anyone who needs to do something more complex (or who needs to inherit from a different base class) can still implement the interface and be plug-and-play.

这样,任何对抽象类中的默认实现感到满意的人都可以快速子类化,而无需重写大量代码,但任何需要做更复杂的事情(或需要从不同的基类继承)的人仍然可以实现界面,即插即用。

回答by Michael Myers

Private fields and methods cannot be used by subclasses (except if they are also inner classes). You could make them protected, however.

子类不能使用私有字段和方法(除非它们也是内部类)。但是,您可以保护它们。

回答by Draemon

An interface defines just that - an interface (contract) with the world outside the implementing class. The public methods of a (abstract or not) superclass do the same. You shouldn't try to require subclasses to have certain private members - you are trying to specify implementation which is what OOP is all about avoiding.

接口定义了这一点 - 与实现类之外的世界的接口(合同)。(抽象或非抽象)超类的公共方法做同样的事情。您不应该试图要求子类具有某些私有成员 - 您正在尝试指定实现,而这正是 OOP 所要避免的。

You use an abstract class when you want to define some shared behaviour in a superclass - but that class can't stand on its own (it needs to be subclassed). It may be that the shared behaviour needs some state of its own - in which case it can define private fields, and it may also have private methods which only it can use.

当您想在超类中定义一些共享行为时,您可以使用抽象类 - 但该类不能独立存在(它需要被子类化)。共享行为可能需要它自己的某种状态——在这种情况下,它可以定义私有字段,并且它也可能具有只有它可以使用的私有方法。

Even if you inherit from an abstract class, you won't be able to access its private fields/methods.

即使您从抽象类继承,您也无法访问其私有字段/方法。

回答by duffymo

Interfaces define a contractfor behavior. That's what they need to be about, not attributes.

接口定义了行为契约。这就是他们需要的东西,而不是属性。

回答by Yes - that Jake.

That's correct. Interfaces are for public consumption. And private implementation needs to be in an abstract (or concrete) class.

没错。接口供公众使用。私有实现需要在抽象(或具体)类中。

回答by Simon Groenewolt

If you ever find yourself guessing which one to choose I's suggest to err on the side of Interfaces - It's better to have an interface that should have been an abstract class than the other way around.

如果您发现自己在猜测该选择哪个,我建议您在接口方面犯错 - 拥有一个应该是抽象类的接口比其他方式更好。

回答by Fabian Steeg

This is correct. But it is not necessarily an either-or decision, you can combine the advantages of interfaces and abstract classes by providing a skeletal implementation along with your interface. You can find a very interesting description of this approach in Effective Java, 2nd Ed., Item 18 ("Prefer interfaces to abstract classes").

这是对的。但这不一定是非此即彼的决定,您可以通过随接口一起提供骨架实现来结合接口和抽象类的优点。您可以在Effective Java, 2nd Ed 中找到对这种方法的非常有趣的描述, 第 18 项(“首选接口而不是抽象类”)。

回答by Steven A. Lowe

I want to force a class to use particular private fields (and methods)
我想强制一个类使用特定的私有字段(和方法)

This part of your question is questionable: why do you think you want to do this?

你问题的这一部分是有问题的:你认为你为什么要这样做?

private members are not visible to subclasses, and interfaces define the public interface, so your only choice would be to use an abstract class...but I cannot think of any reason why anyone would ever want to do this

私有成员对子类不可见,接口定义了公共接口,所以你唯一的选择是使用抽象类......但我想不出任何人会想要这样做的任何理由

回答by Lars Andren

If you want a class to use certain fields or methods of another class, you could declare them as protected.

如果您希望一个类使用另一个类的某些字段或方法,您可以将它们声明为受保护的。

    public abstract class A {

      protected Object thing;
    }

can be accessed by another class in the same package (could be extending class A, or not)

可以被同一个包中的另一个类访问(可以是扩展类 A,也可以不是)

A a = new A();
a.thing.toString();

It's not really "forcing" another class to use it though, more like "enabling".

不过,这并不是真正“强迫”另一个类使用它,更像是“启用”。