是否可以创建具有大量静态字段的Constants类?

编程入门 行业动态 更新时间:2024-10-27 04:35:47
本文介绍了是否可以创建具有大量静态字段的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类?

本文发布于:2023-11-08 11:45:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569282.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   静态   Constants

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!