繁体   English   中英

变量在方法中定义了 2 次

[英]Variable defined 2 times in the method

当我使用这种方法时,我正在审查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);
}

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

其实不是这样的。

您正在检查反编译的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);
    }

但是,如果您检查源文件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);
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM