简体   繁体   English

是否可以在接口中实现本地方法?

[英]Is it possible to implement native methods in interfaces?

You often hear that methods in an interface have no implementation.您经常听说接口中的方法没有实现。 However, in Java 8 it became possible to implement the default methods.然而,在 Java 8 中,可以实现默认方法。 But I'm interested.但我很感兴趣。 Was and is it possible to implement interface methods natively?是否可以本地实现接口方法? ( native methods). native方法)。

When the interview question is asked - "Is it possible to implement a method in an interface?"当面试问题被问到——“是否可以在接口中实现一个方法?” Answer - you can make a native method implementation, and since Java 8 it is possible to define a default method." How correct is this answer?答案 - 您可以实现本机方法,并且由于 Java 8 可以定义默认方法。”这个答案有多正确?

No, interfaces can't have native methods :不,接口不能有native方法

Note that an interface method may not be declared [..] with the modifiers final , synchronized , or native .请注意,不能使用修饰符finalsynchronizednative声明接口方法 [..] 。

Technically that text is non-normative, because it only points out that this list does not contain the mentioned modifiers:从技术上讲,该文本是非规范的,因为它仅指出此列表不包含提到的修饰符:

InterfaceMethodModifier :接口方法修饰符
(one of) (之一)
Annotation public private注释public private
abstract default static strictfp abstract default static strictfp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM