简体   繁体   English

如何更改“ private static final”参数?

[英]How to vary “private static final” parameter?

I am new to java and object oriented programming and I have a question. 我是Java和面向对象程序设计的新手,我有一个问题。 There is a huge java code which has multiple packages and scrapbooks (each has several classes). 有一个巨大的Java代码,其中包含多个包和剪贴簿(每个都有多个类)。 It has many "private static final" parameters with their values. 它具有许多带有其值的“私有静态最终”参数。 Now, I need to make those values variable which means I need to change the value of those parameters and each time, run the code to get the result. 现在,我需要使这些值可变,这意味着我需要更改这些参数的值,并且每次运行代码以获取结果。 It wont be easy if I change the code each time and run the code. 如果我每次都更改代码并运行代码,这将不容易。 I want to give the numbers one time to the code (like list of numbers) and run the code for that. 我想一次将数字提供给代码(例如数字列表)并为此运行代码。 Any idea how to do that with least changes to the current code? 知道如何在最少更改当前代码的情况下执行此操作吗? For example define new class and give all variables in that class as array and with a for loop like K_value=K(i) and change the code from eg "private static final int k = 10" to "private static final int k= K_value". 例如,定义新类,并将该类中的所有变量指定为数组,并使用for循环,例如K_value = K(i),然后将代码从“ private static final int k = 10”更改为“ private static final int k = K_value” ”。 To make it clear lets assume: 为了清楚起见,我们假设:

private static final K= 5
private static final Q= -5
private static final M= 1

and K can be:5, 10, 15 Q can be -5, -10, -15, -20 and M can be 1, 2 and I want to run the code with all/partial combinations of above values. 和K可以是:5、10、15 Q可以是-5,-10,-15,-20和M可以是1、2,并且我想使用上述值的全部/部分组合来运行代码。 eg: 1) K=5, Q=-5, M=1 and 2) K=5, Q=-5, M=2 3) K=5, Q=-10, M=1 and so on. 例如:1)K = 5,Q = -5,M = 1和2)K = 5,Q = -5,M = 2 3)K = 5,Q = -10,M = 1,依此类推。

I will answer your question in regards to the least amount of changes requirement. 我将回答有关least amount of changes要求的问题。 Any approach that is 'quick and dirty' will be marked with @@@ and I would urge you to not use them in a production scenario (eg just use them for one-off testing purposes or some old inherited non-critical code). 任何“快速且肮脏的”方法都将标有@@@ ,我敦促您不要在生产方案中使用它们(例如,仅将它们用于一次性测试目的或某些旧的继承的非关键代码)。 I will express some general thoughts about the outlined scenario afterwards. 之后,我将对概述的场景表达一些一般性想法。

  1. Read an environment variable (or system property) into the static final variables and start the program multiple times with a different value for its environment variable / system property: private static final int someConst = Integer.parseInt(System.getEnv("someConst")) @@@ 将环境变量(或系统属性)读取到静态最终变量中,并使用其环境变量/系统属性的不同值多次启动程序: private static final int someConst = Integer.parseInt(System.getEnv("someConst")) @@@

  2. Make the variable non final and change the value after every run of your suggested for loop method. 使变量为非最终变量,并在每次运行建议的for循环方法后更改该值。 Please note that this is extremely fragile/dangerous/error-prone as the variables may only read once at eg the application startup @@@ 请注意,这非常脆弱/危险/容易出错,因为变量只能在应用程序启动@@@读取一次。

  3. Rewrite the components that use the given constants to require the value as a constructor or function parameter. 重写使用给定常量的组件,以将值用作构造函数或函数参数。 Depending on the applications size or how confident you feel in refactoring an unknown code base this could prove to be difficult. 根据应用程序的大小或您对重构未知代码库的信心,这可能很困难。 Consider using this approach if you have a well tested application 如果您有经过良好测试的应用程序,请考虑使用此方法

When reading your question I noticed that the outlined scenario seems like an algorithm/logic which should operate on input values instead of constants (you described they are not constant anymore). 在阅读您的问题时,我注意到概述的场景似乎是一种算法/逻辑,应该对输入值而不是常量进行操作(您描述了它们不再是常量)。 If possible the code should be restructured to reflect these new circumstances: make it instantiatable (add a constructor parameter) for different values or add a parameter to the affected functions/classes. 如果可能的话,应该对代码进行重组以反映这些新情况:使其可实例化(添加构造函数参数)以获取不同的值,或者向受影响的函数/类添加参数。 All of the suggested fixes are just workarounds to modify as little code as possible. 所有建议的修复程序仅是解决方法,可修改尽可能少的代码。 My advice: rewrite this if the usage scenario is for a production environment and use one of the outlined hacky workarounds if this is a one-off tutorial/testing/exploration. 我的建议:如果使用场景是针对生产环境的,请重写此代码;如果这是一次性的教程/测试/探索,请使用概述的变通办法之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM