Java:接口是否包含在其中定义的常量变量? [英] Java: Can interfaces contain constant variables defined in them?

查看:183
本文介绍了Java:接口是否包含在其中定义的常量变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在界面中创建 public static final 变量吗?我可以保留这些文件中定义的常用常量值吗?

Can I can create public static final variables in an interface? Can I keep some common constant values defined in these files?

推荐答案

是的,您可以:

public interface Constants
{
    public static final int ZERO = 0;
}

然而,通常认为不是这些天好主意。如果接口具有真正的以及,并且大多数实现都可能使用常量......但是引入接口只是,这并不是那么糟糕更容易获得常量是滥用接口的目的,真的。 (这就是过去经常发生的事情。)

However, it's generally reckoned not to be a good idea these days. It's not so bad if the interface has a real purpose as well, and the constants are likely to be used by most of the implementations... but introducing an interface just to make it easier to get to constants is an abuse of the purpose of interfaces, really. (And that's what used to happen a lot.)

这篇关于Java:接口是否包含在其中定义的常量变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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