为什么java接口不能包含静态方法实现?
我只是好奇,允许接口包含静态方法的实现不是更方便吗?此类方法可以包含简短的常用(由该接口实现者)逻辑。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为接口描述了什么。它没有描述如何。
Because an interface describes what. It doesn't describe how.
如果您确实想要在接口内添加(隐藏)一些逻辑,您可以考虑添加一个内部类(注意:永远不要这样做,这只是显示从纯技术角度来看什么是可能的):
如果当你使用它时,它“感觉”有点像在接口中使用静态方法代码:
正如我所说 - 它是纯粹的技术,我看不出任何实际这样做的理由。静态方法可以位于任何类中,无需将其添加到接口中。
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):
If you use this, it "feels" a bit like having static method code in the interface:
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.
接口是一个契约。它说明了实现对象(至少)将具有什么,但仅此而已。它说“这所房子将有一扇门、一扇窗户和一个烟囱”。
抽象类更像是预制房屋。它并不完整(例如,您必须添加自己的壁板),但它已经有部件了(门有一个空间,但整个壁炉已经设置好了。
在接口中提供代码的问题是多重继承。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).
我同意静态方法在接口中没有意义。但我不明白为什么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.
它是应该实现某些东西的抽象类或常规类。接口不应该有任何实现,但它们包含通信的接口。所以静态方法是不允许的。
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.
接口是一个特殊的抽象类,具有所有抽象方法。
您可以随意创建自己的抽象类,其中包含(非抽象)静态方法,但您只能从其中之一继承。
更好的是,使用静态方法创建一个单独的帮助器类。
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.