Java根据参数选择要初始化的类。

huangapple go评论90阅读模式
英文:

Java Select which class to initialize by parameter

问题

这是一个关于测试字符串的代码,包括一个主要的 TestConstants 类,其中存储了用于测试的字符串。

  1. public class TestConstants {
  2. public static final STRING_TEXT;
  3. }

然后,有一个子类 EnglishConstants。如果应用程序使用英语,则将使用这个类:

  1. public class EnglishConstants extends TestConstants {
  2. public static final STRING_TEXT = "Text in English.";
  3. }

最后,还有一个子类 PortugueseConstants。如果应用程序使用葡萄牙语,则将使用这个类:

  1. public class PortugueseConstants extends TestConstants {
  2. public static final STRING_TEXT = "Text in Portuguese.";
  3. }

目的是,如果在终端上使用 Maven 构建测试时提供了 "-Dlanguage=" 属性,并根据该属性选择在该语言中的 "STRING_TEXT"。

所以,如果提供了以下命令:

  1. -Dlanguage=pt-pt

那么测试将使用 "TestConstants.STRING_TEXT",并从 "PortugueseConstants" 类中获取葡萄牙语的值。

英文:

There is a main TestConstant class where the strings for testing are stored.

  1. public class TestConstants {
  2. public static final STRING_TEXT;
  3. }

Then, there is a child class EnglishConstants. This class will be used if application is using English language:

  1. public class EnglishConstants extends TestConstants {
  2. public static final STRING_TEXT = "Text in English.";
  3. }

At the end, there is a child class PortugueseConstants. This class will be used if application is using Portuguese language:

  1. public class PortugueseConstants extends TestConstants {
  2. public static final STRING_TEXT = "Text in Portuguese.";
  3. }

What's the point:
We want if it is possible, when running the test by Maven build from terminal with "-Dlanguage=" property provided, and based on that property "STRING_TEXT" to be selected in that language.
So, if the:

  1. -Dlanguage=pt-pt is provided

Test has then to use "TestConstants.STRING_TEXT" and to take the value in the Portuguese language from the "PortugueseConstants" class.

Tried with factory on this question and answer:
https://stackoverflow.com/a/28602106/9074348

答案1

得分: 0

以下是您要翻译的部分:

"After all investigation I've decided to ask ChatGPT for the help. So, now we have an proper solution for selecting cross-language constants.
Solution:

  1. public class TestConstants {
  2. public static String STRING_TEXT;
  3. public static String NEW_TEXT;
  4. public static String ANOTHER_TEXT;
  5. }
  1. public class EnglishConstants extends TestConstants {
  2. static {
  3. STRING_TEXT = "String Text in English.";
  4. NEW_TEXT = "New Text in English.";
  5. ANOTHER_TEXT = "Another Text in English.";
  6. }
  7. }
  1. public class PortugueseConstants extends TestConstants {
  2. static {
  3. STRING_TEXT = "String Text in Portuguese.";
  4. NEW_TEXT = "New Text in Portuguese.";
  5. ANOTHER_TEXT = "Another Text in Portuguese.";
  6. }
  7. }
  1. public class ConstantsFactory {
  2. public static TestConstants getLanguageConstants(String language) {
  3. switch (language) {
  4. case "pt-pt":
  5. return new PortugueseConstants();
  6. case "en-us":
  7. default:
  8. return new EnglishConstants();
  9. }
  10. }
  11. }
  1. // Usage example
  2. public class MainClass {
  3. public static void main(String[] args) {
  4. String language = System.getProperty("language");
  5. // Initialization of the TestConstant as language by parameter can be stored //in the BaseTest also
  6. TestConstants languageConstants = ConstantsFactory.getLanguageConstants(language);
  7. String stringText = languageConstants.STRING_TEXT;
  8. String newText = languageConstants.NEW_TEXT;
  9. String anotherText = languageConstants.ANOTHER_TEXT;
  10. System.out.println("String Text: " + stringText);
  11. System.out.println("New Text: " + newText);
  12. System.out.println("Another Text: " + anotherText);
  13. }
  14. }

如果要运行一些测试,请使用:

  1. mvn clean test -Dlanguage=en-en
英文:

After all investigation I've decided to ask ChatGPT for the help. So, now we have an proper solution for selecting cross-language constants.
Solution:

  1. public class TestConstants {
  2. public static String STRING_TEXT;
  3. public static String NEW_TEXT;
  4. public static String ANOTHER_TEXT;
  5. }
  1. public class EnglishConstants extends TestConstants {
  2. static {
  3. STRING_TEXT = "String Text in English.";
  4. NEW_TEXT = "New Text in English.";
  5. ANOTHER_TEXT = "Another Text in English.";
  6. }
  7. }
  1. public class PortugueseConstants extends TestConstants {
  2. static {
  3. STRING_TEXT = "String Text in Portuguese.";
  4. NEW_TEXT = "New Text in Portuguese.";
  5. ANOTHER_TEXT = "Another Text in Portuguese.";
  6. }
  7. }
  1. public class ConstantsFactory {
  2. public static TestConstants getLanguageConstants(String language) {
  3. switch (language) {
  4. case "pt-pt":
  5. return new PortugueseConstants();
  6. case "en-us":
  7. default:
  8. return new EnglishConstants();
  9. }
  10. }
  11. }
  1. // Usage example
  2. public class MainClass {
  3. public static void main(String[] args) {
  4. String language = System.getProperty("language");
  5. // Initialization of the TestConstant as language by parameter can be stored //in the BaseTest also
  6. TestConstants languageConstants = ConstantsFactory.getLanguageConstants(language);
  7. String stringText = languageConstants.STRING_TEXT;
  8. String newText = languageConstants.NEW_TEXT;
  9. String anotherText = languageConstants.ANOTHER_TEXT;
  10. System.out.println("String Text: " + stringText);
  11. System.out.println("New Text: " + newText);
  12. System.out.println("Another Text: " + anotherText);
  13. }
  14. }

If you want to run the some test use:

  1. mvn clean test -Dlanguage=en-en

huangapple
  • 本文由 发表于 2023年5月17日 22:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定