接口没有方法 [英] Interface with no methods

查看:198
本文介绍了接口没有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java引入了一些没有定义方法的接口?例如可克隆可序列化键入等等。

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

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

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. Why so? and Why Java need some method without body to be called from static block.?

推荐答案


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

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

这称为标记或标记界面。这些不用于任何使用或操作。这些方法用于标记或标记类。

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 detereming whether someclass is a child of those classe.


关于第二个问题

about the second question

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

if you look closely you can see the declartion is

 private static native void registerNatives();

因此registerNatives是一种本地方法。

So registerNatives is a native methods.

所以什么是本地方法。如果您看到这样的问题< a>

so what is native methods. if you see this so question


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

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.

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

So this methods are loaded from native codes. So you need not declare the body of the methods but still they are not abstract as they have their implementation from native codes.

这篇关于接口没有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆