是否可以创建具有大量静态字段的Constants类? [英] Is it OK to create a Constants class with large number of static fields?

查看:203
本文介绍了是否可以创建具有大量静态字段的Constants类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含200多个静态字段的常量类:

Let's say I have a constants class containing 200+ static fields :

class AnimalConstants {
  static final int AARDVARK = 1;
  static final int ANTELOPE = 2;
  static final int BEAR = 3;
  ...
  ...
  static final int ZEBRA = 200;
}




  • 任何人都可以解释是否有对性能和内存的负面影响

  • 如果类更改为接口(例如SwingConstants)并由某些类实现,会更好还是更糟?

  • 如果我将常量实现为Enum类,更好或更差?

    • Can anyone explain if there are any negative impact on performance and memory from using such classes.
    • Would it be better or worse if the class is changed to an interface (e.g. SwingConstants) and being implemented by some classes?
    • Would it be better or worse if I implement the constants as an Enum class?
    • 推荐答案

      认为影响是性能或内存。

      I don't think the impact is performance or memory.

      我认为它必须与代码的可读性,保持常数接近他们使用的地方,以及对你的问题的基本理解。

      I think it has to do with things like readability of code, keeping constants close to where they're used, and fundamental understanding of your problem.

      我喜欢声明常量更接近他们使用的地方。我相信他们更容易理解。

      I prefer to declare constants closer to where they're used. I believe they're easier to understand that way.

      如果你声称为200+的真实常数是真正相关的,我会很惊讶。如果他们是,他们属于一起在一个地方。如果没有,我会说他们应该被分成更小的块,并宣布更接近他们使用的地方。

      I would be surprised if the real constants that you claim number 200+ are truly related. If they are, they belong together in one place. If not, I'd say that they should be broken into smaller pieces and declared closer to where they're used.

      我敢打赌,如果已知,会改变响应的示例。

      I'll bet there's more context than your contrived example that would change responses if known.

      当然,枚举是伟大的。但请先查看我的其他意见。

      Sure , enums are great. But see my other comments first.

      这篇关于是否可以创建具有大量静态字段的Constants类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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