我们可以编写自定义标记接口 [英] can we write custom marker interfaces

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

问题描述

我想编写自己的标记接口,例如 java.io.Serializable Cloneable 这可以理解为JVM。请建议我实现过程。

I want to write my own marker interfaces like java.io.Serializable or Cloneable which can be understandable to JVM. Please suggest me the implementation procedure.

例如,我实现了一个名为 NotInheritable 的接口以及实现此接口的所有类必须避免继承。

For example I implemented a interface called NotInheritable and all the classes implementing this interface has to avoid inheritance.

推荐答案

public interface MyMarkerInterface {}

public class MyMarkedClass implements MyMarkerInterface {}

然后你可以让方法只取 MyMarkerInterface instance:

Then you can for example have method taking only MyMarkerInterface instance:

public myMethod(MyMarkerInterface x) {}

或在运行时使用 instanceof 进行检查。

or check with instanceof at runtime.

这篇关于我们可以编写自定义标记接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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