変数は、この方法では2回定義しました

シーナ:

私は中にいくつかの方法を見直した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回定義されてコンパイルされるだろうか?これは、ライブラリみんなの使用の一部です。

Rajnish suryavanshi:

実際にはそのようではありません。

あなたは逆コンパイルにチェックされている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);
  }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=330177&siteId=1