gpt4 book ai didi

java - 变量在方法中定义了 2 次

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:44:03 25 4
gpt4 key购买 nike

当我想到这个方法时,我正在审查 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com