java - 没有方法的接口(interface)

标签 java class interface java-native-interface

为什么Java会引入一些没有定义方法的接口(interface)?例如 CloneableSerializableType 等等。

第二件事:在 Class.class 包中,有一个方法定义了 registerNatives() 没有正文,它是从静态 block 调用的,但是 Class.class 不是抽象的,而是 final。为什么这样? 以及为什么 Java 需要一些没有主体的方法才能从静态 block 中调用。?

最佳答案

Why do Java introduces some interface which has no methods defined in it?

这叫做 Tagged or Marker interface .这些不用于任何用途或操作。这些方法用于标记或标记一个类。这样您就可以确定某个类是否是这些类的子类。

about the second question

如果你仔细看,你会发现声明是

 private static native void registerNatives();

所以registerNatives是一个本地方法。

那么什么是原生方法。如果你看到 this so question

The method is implemented in "native" code. That is, code that does not run in the JVM. It's typically written in C or C++.

Native methods are usually used to interface with system calls or libraries written in other programming languages.

所以这些方法是从本地代码加载的。所以你不需要声明方法的主体,但它们仍然不是抽象的,因为它们是从本地代码实现的。

关于java - 没有方法的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20878039/

相关文章:

java - 使用存储库的异步方法中的 Spring InvalidDataAccessApiUsageException

java - 用java解码url

c# - 调用对象的 ContainsKey 后字典抛出 StackOverflowException

objective-c - ios多类实现

java - 处理同名继承方法的规则是什么?

java - 原子到底是什么?

java - 如何创建 Android Facebook key 哈希?

c# - 关于类的属性更改时如何使 UI 使用react的任何想法?

oop - 使用接口(interface)的真正好处是什么?

c# - 如何重载 [] 运算符?