接口中的字段 [英] Fields in interfaces

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

问题描述

我在Java中有一个基本问题,但这是OOP中的一个普遍问题。为什么接口允许设置字段?这与那个界面应该做的不相反吗?

I have a basic question in Java, but it's a general question in OOP. Why do interfaces allow fields to be set? Doesn't that run contrary to what an interface is supposed to do?

我理解它的方式,界面是英文的形容词。所以,如果我的类实现了Runnable和Serializable接口,我确保用户我的类将满足Runnable和Seriablizable的条件。但是,这意味着接口是无状态的,但允许它们在Java中有字段......

The way I made sense of it, an interface is what in English would be an adjective. So, if my class implements the interfaces Runnable and Serializable, I'm ensuring the user that my class will satisfy the conditions to be Runnable and Seriablizable. However, that would mean interfaces are "stateless", but they are allowed to have fields in Java...

我错过了什么?

推荐答案

界面中的所有字段都是 public static final ,即它们是常量。

All fields in interface are public static final, i.e. they are constants.

通常建议避免使用此类接口,但有时您可以找到一个没有方法的接口,并且仅用于包含常量值列表。

It is generally recommended to avoid such interfaces, but sometimes you can find an interface that has no methods and is used only to contain list of constant values.

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

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