private static String[][] s1 = {
{ "a", "a1" },
{ "b", "b1" },
{ "c", "c1" },
{ "d", "d1" },
}
private static String[][] s2 = {
{ "c", "c1" },
{ "d", "d1" },
{ "e", "e1" },
{ "f", "f1" },
}
这里 c,c1 和 d,d1 在这些字符串数组中重复。 如何将其取出并放入公共(public)字符串数组(例如 s3)中,并且 s1 和 s2 重用/继承它,以便不需要在两个不同的位置声明它?
最佳答案
static String[] common = { "c", "c1" };
private static String[][] s1 = {
{ "a", "a1" },
{ "b", "b1" },
common,
{ "d", "d1" }
}
private static String[][] s2 = {
common,
{ "d", "d1" },
{ "e", "e1" },
{ "f", "f1" }
}
工作正常。
关于java - 在 Java/继承中重用静态字符串数组,就像字符串数组中的概念一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21129287/