Java 没有方法的接口

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

Interface with no methods

javaclassinterfacejava-native-interface

提问by Vimal Bera

Why do Java introduces some interface which has no methods defined in it? For example Cloneable, Serializable, Typeand many more.

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

Second thing : In Class.classpackage there is one method defined registerNatives()without body and is called from static block but Class.classis not abstract but is final. Why so? and Why Java need some method without body to be called from static block.?

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

采纳答案by stinepike

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

为什么Java引入了一些没有定义方法的接口?

This are called Tagged or Marker interface. These are not used for any use or operation. These methods are used to tag or marking a class. So that you can determine whether someclass is a child of those classes.

这称为标记或标记接口。这些不用于任何用途或操作。这些方法用于标记或标记一个类。这样您就可以确定 someclass 是否是这些类的子类。

about the second question

关于第二个问题

If you look closely you can see the declaration is

如果你仔细观察,你可以看到声明是

 private static native void registerNatives();

So registerNativesis a native methods.

registerNatives本机方法也是如此。

So what is native methods. If you see 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.

该方法是在“本机”代码中实现的。也就是说,不在 JVM 中运行的代码。它通常用 C 或 C++ 编写。

本机方法通常用于与系统调用或用其他编程语言编写的库进行交互。

So these methods are loaded from native codes. So you don't need to declare the body of the methods but still they are not abstract as they have their implementation from native codes.

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

回答by rachana

Marker interfaceis used as a tag to inform a message to the java compiler so that it can add special behavior to the class implementing it. Java marker interface has no members in it.

标记接口用作标记来通知 java 编译器的消息,以便它可以向实现它的类添加特殊行为。Java 标记接口中没有成员。

The purpose of Marker interfacesis to force some kind of functionality in the classes by providing some functionality to a class if it implements the marker interface.

标记接口的目的是通过向实现标记接口的类提供某些功能来强制类中的某种功能。

Read Java Marker Interfacealso see What is the use of marker interfaces in Java?

阅读Java Marker Interface也请参阅Java 中标记接口的用途是什么?

回答by Aniket Thakur

registerNatives()

native method are implemented in JVM itself. What does the registerNatives() method do?

本机方法是在 JVM 本身中实现的。 registerNatives() 方法有什么作用?

Why Java need some method without body to be called from static block.?

This is called from static block because we need to call this method when classes are loaded and not when it's instance is created.

这是从静态块调用的,因为我们需要在加载类而不是在创建实例时调用此方法。

回答by Nazgul

For the first one you are actually asking for a Marker Interface. Marker Interfaces are by design not supposed to add anything to behavior but support only polymorphic transformation of the object. e.g. Serializable makes an object capable of streaming across JVM boundaries. Marker interfaces follow the 'universal type substitution' philosophy.

对于第一个,您实际上是在要求一个标记界面。标记接口的设计不应该向行为添加任何内容,而只支持对象的多态转换。例如,Serializable 使对象能够跨 JVM 边界进行流式传输。标记接口遵循“通用类型替换”理念。

For second one, you are actually asking for JNI. Java doesnot implement all its code in Java form. I mean in classes and code that follow Java syntax. Some time or the other you need to drill down to the native platform API to implement something for that API. e.g. sockets and TCP communication. It is this feature of Java that actually makes it platform independent. The JVM runtime is platform dependent as it uses platform based native methods and dll or .so libraries to implement and integrate with the platform. We as programmers call the high level Java SDK API calls.

对于第二个,您实际上是在要求 JNI。Java 并未以 Java 形式实现其所有代码。我的意思是在遵循 Java 语法的类和代码中。有时您需要深入到本机平台 API 来为该 API 实现一些东西。例如套接字和 TCP 通信。正是 Java 的这一特性使其独立于平台。JVM 运行时依赖于平台,因为它使用基于平台的本机方法和 dll 或 .so 库来实现并与平台集成。我们作为程序员调用高级 Java SDK API 调用。

回答by Bipil Raut

One of the "clean" features of the Java programming language is that it mandates a separation between interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to specify the behavior of derived classes. Often you will come across interfaces in Java that have no behavior. In other words, they are just empty interface definitions. These are known as marker interfaces. Some examples of marker interfaces in the Java API include:

Java 编程语言的“干净”特性之一是它要求在接口(纯行为)和类(状态和行为)之间进行分离。Java 中使用接口来指定派生类的行为。您经常会遇到 Java 中没有行为的接口。换句话说,它们只是空的接口定义。这些被称为标记接口。Java API 中标记接口的一些示例包括:

  • java.lang.Cloneable
  • java.io.Serializable
  • java.util.EventListener
  • java.lang.Cloneable
  • java.io.Serializable
  • java.util.EventListener

Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose. For example, all classes that implement the Cloneable interface can be cloned (i.e., the clone() method can be called on them). The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. For example, consider the following call to the clone() method on an object o:

标记接口也称为“标记”接口,因为它们根据用途将所有派生类标记为一个类别。例如,所有实现 Cloneable 接口的类都可以被克隆(即可以对它们调用 clone() 方法)。Java 编译器检查以确保是否在类上调用了 clone() 方法并且该类是否实现了 Cloneable 接口。例如,考虑以下对对象 o 上的 clone() 方法的调用:

SomeObject o = new SomeObject();
SomeObject ref = (SomeObject)(o.clone());

If the class SomeObject does not implement the interface Cloneable (and Cloneable is not implemented by any of the superclasses that SomeObject inherits from), the compiler will mark this line as an error. This is because the clone() method may only be called by objects of type "Cloneable." Hence, even though Cloneable is an empty interface, it serves an important purpose.

如果类 SomeObject 没有实现接口 Cloneable(并且 Cloneable 没有被 SomeObject 继承的任何超类实现),编译器会将此行标记为错误。这是因为 clone() 方法只能由“Cloneable”类型的对象调用。因此,即使 Cloneable 是一个空接口,它也有重要用途。