简体   繁体   English

没有方法的接口

[英]Interface with no methods

Why do Java introduces some interface which has no methods defined in it? 为什么Java引入了一些没有定义方法的接口? For example Cloneable , Serializable , Type and many more. 例如CloneableSerializableType等等。

Second thing : In Class.class package there is one method defined registerNatives() without body and is called from static block but Class.class is not abstract but is final . 第二件事:在Class.class包中有一个方法定义了registerNatives()没有body,从静态块调用,但Class.class不是抽象的,而是final Why so? 为什么这样? and Why Java need some method without body to be called from static block.? 为什么Java需要一些没有body的方法来从静态块中调用。

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. 这样您就可以确定某些类是否是这些类的子类。

about the second question 关于第二个问题

If you look closely you can see the declaration is 仔细观察,您可以看到声明

 private static native void registerNatives();

So registerNatives is 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. 也就是说,代码不能在JVM中运行。 It's typically written in C or C++. 它通常用C或C ++编写。

Native methods are usually used to interface with system calls or libraries written in other programming languages. 本机方法通常用于与系统调用或用其他编程语言编写的库进行交互。

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. 因此,您不需要声明方法的主体,但它们仍然不是抽象的,因为它们是从本机代码实现的。

Marker interface is 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编译器,以便它可以向实现它的类添加特殊行为。 Java marker interface has no members in it. Java标记接口中没有成员。

The purpose of Marker interfaces is to force some kind of functionality in the classes by providing some functionality to a class if it implements the marker interface. Marker接口的目的是通过在类实现标记接口时为类提供某些功能来强制类中的某种功能。

Read Java Marker Interface also see What is the use of marker interfaces in Java? 阅读Java Marker Interface还看看Java中标记接口 的用途是什么?

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. 标记接口在设计上不应该向行为添加任何内容,而只支持对象的多态转换。 eg Serializable makes an object capable of streaming across JVM boundaries. 例如,Serializable使对象能够跨JVM边界进行流式传输。 Marker interfaces follow the 'universal type substitution' philosophy. 标记接口遵循“通用类型替换”理念。

For second one, you are actually asking for JNI. 对于第二个,你实际上是在要求JNI。 Java doesnot implement all its code in Java form. Java不会以Java形式实现其所有代码。 I mean in classes and code that follow Java syntax. 我的意思是遵循Java语法的类和代码。 Some time or the other you need to drill down to the native platform API to implement something for that API. 您需要深入了解本机平台API以实现该API的某些时间。 eg sockets and TCP communication. 例如套接字和TCP通信。 It is this feature of Java that actually makes it platform independent. Java的这个特性实际上使它与平台无关。 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. JVM运行时依赖于平台,因为它使用基于平台的本机方法和dll或.so库来实现和集成平台。 We as programmers call the high level Java SDK API calls. 我们作为程序员调用高级Java SDK API调用。

registerNatives()

native method are implemented in JVM itself. 本机方法在JVM本身中实现。 What does the registerNatives() method do? 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. 这是从静态块调用的,因为我们需要在加载类时调用此方法,而不是在创建实例时调用此方法。

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). Java编程语言的一个“干净”特性是它要求在接口(纯行为)和类(状态和行为)之间进行分离。 Interfaces are used in Java to specify the behavior of derived classes. 接口在Java中用于指定派生类的行为。 Often you will come across interfaces in Java that have no behavior. 通常,您会遇到没有行为的Java接口。 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 API中的标记接口的一些示例包括:

  • java.lang.Cloneable java.lang.Cloneable
  • java.io.Serializable 的java.io.Serializable
  • java.util.EventListener 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 (ie, the clone() method can be called on them). 例如,可以克隆实现Cloneable接口的所有类(即,可以在它们上调用clone()方法)。 The Java compiler checks to make sure that if the clone() method is called on a class and the class implements the Cloneable interface. Java编译器检查以确保在类上调用clone()方法并且该类实现Cloneable接口。 For example, consider the following call to the clone() method on an object o: 例如,考虑以下对对象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. 如果类SomeObject没有实现Cloneable接口(并且Cloneable不是由SomeObject继承的任何超类实现的),则编译器会将此行标记为错误。 This is because the clone() method may only be called by objects of type "Cloneable." 这是因为clone()方法只能由“Cloneable”类型的对象调用。 Hence, even though Cloneable is an empty interface, it serves an important purpose. 因此,即使Cloneable是一个空接口,它也是一个重要的目的。

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

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