【发布时间】:2014-05-07 16:00:41
【问题描述】:
...
/**
* The color white. In the default sRGB space.
*/
public final static Color white = new Color(255, 255, 255);
/**
* The color white. In the default sRGB space.
* @since 1.4
*/
public final static Color WHITE = white; // My comment: THE SAME!!??
...
从上面的摘录中可以看出,Color white 被分配给 两个 变量,即 Color#WHITE 和 Color#white 这也是同样的情况:
- black (and BLACK)
- blue (and BLUE)
- cyan (and CYAN)
- darkGray (and DARK_GRAY)
- gray (and GRAY)
- green (and GREEN)
- lightGray (and LIGHT_GRAY)
- magenta (and MAGENTA)
- orange (and ORANGE)
- pink (and PINK)
- red (and RED)
->white (and WHITE)<-discussed
- yellow (and YELLOW)
最初,我曾经认为每种颜色都有两个名称是有原因的。但是当我查看source code 时,我才知道它们的值相同!
我想知道为什么要为每种颜色设置两个变量?
这种用法是否有任何具体原因(历史原因、实际原因等)?
最后,在我们的应用程序中使用这两者中的哪一个?:
// THIS?:
Color newC = Color.white;
// OR THIS?:
Color newC = Color.WHITE;
【问题讨论】: