JAVA奇葩语法:maker interface

Java Marker Interface(在java与模式一书中翻译作  标识接口)

是一个用来给编译器提供标记的接口,是个空接口。
比如说JDK里面的

Java Marker Interface Examples

  • java.lang.Cloneable
  • java.io.Serializable
  • java.util.EventListener
作用:Marker interface is used as a tag to inform a message to the java compiler so that it can add special behaviour to the class implementing it. Java marker interface has no members in it.
翻译:标识接口是用来作为一种标记给java编译器的,它用来通知java编译器为它自己本身添加上特有的行为,并实现着些特有的行为。
java的标识接口没有任何成员,包括变量和方法。

比如说,Lets take the java.io.Serializable marker interface. It doesnot has any members defined it it. When a java class is to be serialized, you should intimate the java compiler in some way that there is a possibility of serializing this java class. In this scenario, marker interfaces are used. The java class which may be serialized has to implement the java.io.Serializable marker interface. In such way, we are intimating the java compiler.

我们拿java.io.Serializable接口作为例子来说明一下。 如果存在一个对象,它实现了java.io.Serializable接口,由于接口本身没有定义任何方法行为。所以实现接口的行为由java编译器来完成。当一个java类实现了这个接口,在编译过程中,java编译器会发现这个类的对象是属于java.io.Serializable这种类型,那么编译器就会为这个特殊的类实现序列化所要求的特殊的行为,使得该类的对象可以在不同虚拟机之间传递。 所以说,我们需要有一个标记的东西 来通知java编译器这个特殊的属性,我们就定义了标识接口。

From java 1.5, the need for marker interface is eliminated by the introduction of the java annotation feature. So, it is wise to use java annotations than the marker interface. It has more feature and advantages than the java marker interface.

Is Runnable a marker interface?

First point, there is no reference or definition about marker interface from Java specification/api. This is yet another inconclusive debate. ‘Marker Interface’ is a term coined by (book / web) authors. Since we don’t have Java’s definition it is left to us to arrive at a decent definition. This is one popular question asked in java interview. My answer to the interviewer, there are better questions ask than this to ask.


“Runnable is not a marker interface”, you might say ‘run’ is used to start a method and this is a special instruction to the JVM.  When you run a java class the java interpreter calls the ‘main’ method, because of this would you say every java class as “marker class”?

‘My’ definition for marker interface

“An interface is called a marker interface when it is provided as a handle by java interpreter to mark a class so that it can provide special behaviour to it at runtime and they do not have any method declarations”.

In the above definition “and they do not have any method declarations” is added because, till now all the marker interfaces provided by java do not have method declarations. I dont think, in future there will be any new marker interfaces added :-)

We cannot create marker interfaces, as you cannot instruct JVM to add special behavior to all classes implementing (directly) that special interface.




网上找了一个例子;

package test1;

interface SomeThing{}
interface Human extends SomeThing{}
interface Animal extends SomeThing{}
class Chinese implements Human{}
class Japanese {}
class Dog implements Animal{}
class Monster implements Human{}

public class Hunter{
	public void fire(Object obj){
		if(obj instanceof Human) return;
		System.out.println("fire the target" + obj.getClass().getName());
	}
	
	public static void main(String[] args) {
		Hunter hunter = new Hunter();
		Object[] objs = new Object[]{new Dog(), new Japanese(), new Chinese(), new Monster(), new SomeThing(){}};
		
		for(int i=0; i<objs.length;i++){
			hunter.fire(objs[i]);
		}
	}
}

说明这种关系,通过Human标记来判断 目标是否应该开枪,如果是Human的接口实现者,即便是妖怪Monster也不开枪,但即便是人,javanese,但是由于没有实现标记接口,照样开枪。

注意 标记接口这个概念在 官方文档中没有出现,是网络上 或是 出书的人为了 描述这种接口设计 所杜撰出来的一个概念。

例子参看网页:http://www.189works.com/article-14205-1.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值