方法中定义了2次变量

Sin*_*ina 3 java android

我在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);
}
Run Code Online (Sandbox Code Playgroud)

如何用定义两次的变量i编译此方法?它是每个人都使用的库的一部分。

Raj*_*shi 10

其实不是那样的。

您正在检入反编译的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);
    }
Run Code Online (Sandbox Code Playgroud)

但是,如果签入源文件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);
  }
Run Code Online (Sandbox Code Playgroud)