当我想到这个方法时,我正在审查 com.google.android.material.tabs.Tablayout
中的一些方法:
private static ColorStateList createColorStateList(int defaultColor, int selectedColor) {
int[][] states = new int[2][];
int[] colors = new int[2];
int i = 0;
states[i] = SELECTED_STATE_SET;
colors[i] = selectedColor;
int i = i + 1;
states[i] = EMPTY_STATE_SET;
colors[i] = defaultColor;
++i;
return new ColorStateList(states, colors);
}
How this method could be compiled with variable i being defined 2 times? It is part of the library everyone use.
最佳答案
其实不是这样的。
您正在 checkin 反编译的 TabLayout.class 文件
private static ColorStateList createColorStateList(int defaultColor, int selectedColor) {
int[][] states = new int[2][];
int[] colors = new int[2];
int i = 0;
states[i] = SELECTED_STATE_SET;
colors[i] = selectedColor;
int i = i + 1;
states[i] = EMPTY_STATE_SET;
colors[i] = defaultColor;
++i;
return new ColorStateList(states, colors);
}
但是,如果您 checkin 源文件 TabLayout.java,您将获得如下代码。
private static ColorStateList createColorStateList(int defaultColor, int selectedColor) {
final int[][] states = new int[2][];
final int[] colors = new int[2];
int i = 0;
states[i] = SELECTED_STATE_SET;
colors[i] = selectedColor;
i++;
// Default enabled state
states[i] = EMPTY_STATE_SET;
colors[i] = defaultColor;
i++;
return new ColorStateList(states, colors);
}
关于java - 变量在方法中定义了 2 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58498882/