如何在Java中编写自己的标记接口? [英] How to write our own marker interface in Java?

查看:125
本文介绍了如何在Java中编写自己的标记接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道java中的标记界面。它用于定义关于类的特定行为。例如,Serializable接口具有将对象存储到字节流及其反向过程的特定功能。但我不知道这个特定行为的实现位置,因为它没有任何方法。

I know marker interface in java. It is used to define a specific behaviour about a class. For example, Serializable interface has the specific ability to store an object into byte stream and its reverse process. But I don't know where this specific behaviour is implemented, because it doesn't have any method in it.


  1. JVM如何调用此方法具体行为?

  2. 如何编写我们自己的标记界面?你能给我一个简单的用户定义标记界面供我理解吗?

  3. 是否可以在标记界面中使用方法?

请指导我解决此问题。

推荐答案


  1. 序列化由 ObjectInputStream ObjectOutputStream 类处理。如果某个类具有特殊的序列化需求,则在 API <中列出要创建的方法。 / A>。反射用于调用这些方法。

  1. Serialization is handled by the ObjectInputStream and ObjectOutputStream classes. If a class has special serialization needs, the methods to create are outlined in the API. Reflection is used to invoke these methods.

与编写任何其他界面的方式相同。

The same way you would write any other interface.

没有什么可以阻止你将方法放在标记界面中。

There's nothing stopping you from putting methods in a marker interface.

现在更常见的做法是使用注释提供相同的元数据标记接口。

The more common practice now is to use annotations to provide the same metadata marker interfaces provide.

这篇关于如何在Java中编写自己的标记接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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