【发布时间】:2021-12-17 13:56:57
【问题描述】:
我正在构建一个应用程序,根据用户从下拉菜单中的选择来打印不同语言的单词。但是,我当前的方法有效,因为我从嵌套类中获取数据并从完全不同的类中请求它,这需要为嵌套类中的每个单词编写一行代码。我怎样才能以更有效/更轻松的方式实现这一目标?以下是上述代码的基本格式:
public class words {
class example {
String english = "example";
String chinese = "example_in_chinese";
}
class example2 {
String english = "example2";
String chinese = "example2_in_chinese";
}
}
public class getWords {
words word = new words();
words.example example = word.new example();
words.example2 example2 = word.new example2();
*/ I have to repeat the above line for each of the 20
words I have in a nested class
/*
}
【问题讨论】:
-
也许使用字典。在 Java 中实现字典的一种方法是使用
java.util.Map来保存一个英文单词作为键和一个中文单词作为值。
标签: java inner-classes