为什么java接口不能包含静态方法实现?

发布于 2024-10-02 04:03:49 字数 58 浏览 4 评论 0原文

我只是好奇,允许接口包含静态方法的实现不是更方便吗?此类方法可以包含简短的常用(由该接口实现者)逻辑。

I'm just curious, wouldn't it be more convinient to allow interfaces to contain implementations of static methods? Such methods could contain short commonly used(by this interface implementors) logic.

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

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

发布评论

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

评论(6

ゝ偶尔ゞ 2024-10-09 04:03:49

因为接口描述了什么。它没有描述如何

Because an interface describes what. It doesn't describe how.

淡莣 2024-10-09 04:03:49

如果您确实想要在接口内添加(隐藏)一些逻辑,您可以考虑添加一个内部类(注意:永远不要这样做,这只是显示从纯技术角度来看什么是可能的):

public interface Person {
  public String getFirstName();
  public String getLastName();
  public class Util {
    public String getName(Person person) {
      return person.getFirstName() + " " + person.getLastName();
    }
  }
}

如果当你使用它时,它“感觉”有点像在接口中使用静态方法代码:

String fullName = Person.Util.getName(this);

正如我所说 - 它是纯粹的技术,我看不出任何实际这样做的理由。静态方法可以位于任何类中,无需将其添加到接口中。

If you really want to add (hide) some logic inside an interface, you may consider adding an inner class (Note: never do it, this just shows what is possible from a pure technical perspective):

public interface Person {
  public String getFirstName();
  public String getLastName();
  public class Util {
    public String getName(Person person) {
      return person.getFirstName() + " " + person.getLastName();
    }
  }
}

If you use this, it "feels" a bit like having static method code in the interface:

String fullName = Person.Util.getName(this);

As I said - it's pure technically and I don't see any reason to actually do it. A static method can be located in any class, no need to add it to an interface.

一曲爱恨情仇 2024-10-09 04:03:49

接口是一个契约。它说明了实现对象(至少)将具有什么,但仅此而已。它说“这所房子将有一扇门、一扇窗户和一个烟囱”。

抽象类更像是预制房屋。它并不完整(例如,您必须添加自己的壁板),但它已经有部件了(门有一个空间,但整个壁炉已经设置好了。

在接口中提供代码的问题是多重继承。Java不允许。你可以让一个类实现许多接口,因为接口只承诺有一个具有给定签名的方法。

如果接口包含代码,那么你可以实现其中的 3 个,每个接口都有一个 myUsefulFunction( 的方法体)。字符串的东西)...现在你不知道调用哪一个,

这就是为什么抽象类可以有方法体(因为你只能扩展一个类),但接口不能(因为你可以实现多个接口)。

An interface is a contract. It says what an implementing object will have (at minimum), but that's all. It says "this house will have a door, a window, and a chimney".

An abstract class is more like a prefab house. It's not complete (you have to add your own siding, for example) but it has parts already there (there is a space for the door, but the whole fireplace is already setup.

The problem with giving code in interfaces is multiple inheritance. Java doesn't allow it. You can have a class implement many interfaces, because interfaces only promise there will be a method with a given signature.

If interfaces held code, then you could implement 3 of them, each with a method body for myUsefulFunction(String thing)... and now you don't know which one gets called.

That's why abstract classes can have method bodys (because you can only extend one class), but interfaces can't (because you can implement multiple interfaces).

淡写薰衣草的香 2024-10-09 04:03:49

我同意静态方法在接口中没有意义。但我不明白为什么java允许接口中的静态成员。看起来有点不一致。

I agree that a static method doesn't make sense in an interface. But i don't understand why java allows static members in an interface. Seems a bit inconsistent.

妥活 2024-10-09 04:03:49

它是应该实现某些东西的抽象类或常规类。接口不应该有任何实现,但它们包含通信的接口。所以静态方法是不允许的。

It's the abstract class or regular class which should implement something. Interfaces are not supposed to have any implementations, but they contain the interface of communicating. So static methods are not allowed.

独夜无伴 2024-10-09 04:03:49

接口是一个特殊的抽象类,具有所有抽象方法。

您可以随意创建自己的抽象类,其中包含(非抽象)静态方法,但您只能从其中之一继承。

更好的是,使用静态方法创建一个单独的帮助器类。

An interface is a special abstract class with all abstract methods.

You can feel free to create an abstract class of your own that contains (non-abstract) static methods, but then you can only inherit from one of them.

Better yet, create a separate helper class with your static methods.

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