可串行标记接口是否包含默认方法? [英] can marker interface like serializable contain default methods?

查看:160
本文介绍了可串行标记接口是否包含默认方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为它不能,因为标记接口原则是没有任何方法,但由于默认方法不是 abstract 我不确定。

I think it can't, because marker interface principle is to not have any methods, but since default methods are not abstract I am not sure.

推荐答案

就Java而言,Marker接口只是一个常规接口。因此,它可以像任何(Java-8)接口一样使用默认方法。

A "Marker" interface is just a regular interface as far as Java is concerned. Thus, it can have default methods just as any (Java-8) interface can.

现在,这是否违反了原则一个Marker界面,我不得不说是的。 Marker接口应该作为各种标志,仅识别一个类符合某些外部标准。现在,它可以是标记界面具有抽象/默认方法,但它将不再纯粹符合定义。

Now, as to whether this violates the principle of a Marker interface, I would have to say yes. A Marker interface should act as a flag of sorts, only identifying that a class meets some external criteria. Now, it can be a Marker interface and have abstract/default methods, but it will no longer purely meet the definition.

来自 Effective Java(第二版)


标记接口是一个包含no的接口方法声明,但仅指定(或标记)实现接口具有某些属性的类。

A marker interface is an interface that contains no method declarations, but merely designates (or "marks") a class that implements the interface as having some property.

这篇关于可串行标记接口是否包含默认方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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