为什么 getRGB() 方法是这样写的?有没有其他方法可以编写 getRGB() 方法?
Why the getRGB() method is written the way it did? Is there an alternative way to write getRGB() method?
我是 Java 的新手。我从 Java Oracle 教程中看到了下面的代码。我很难理解这段代码的用途:
public synchronized int getRGB() {
return ((red << 16) | (green << 8) | blue);
}
我了解按位运算符的工作原理,但我很难理解为什么 getRGB() 方法是这样编写的。是否有其他方法来编写 getRGB() 方法?
public class SynchronizedRGB {
// Values must be between 0 and 255.
private int red;
private int green;
private int blue;
private String name;
private void check(int red,
int green,
int blue) {
if (red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
throw new IllegalArgumentException();
}
}
public SynchronizedRGB(int red,
int green,
int blue,
String name) {
check(red, green, blue);
this.red = red;
this.green = green;
this.blue = blue;
this.name = name;
}
public void set(int red,
int green,
int blue,
String name) {
check(red, green, blue);
synchronized (this) {
this.red = red;
this.green = green;
this.blue = blue;
this.name = name;
}
}
public synchronized int getRGB() {
return ((red << 16) | (green << 8) | blue);
}
public synchronized String getName() {
return name;
}
public synchronized void invert() {
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
name = "Inverse of " + name;
}
public static void main(String[] args) {
SynchronizedRGB color = new SynchronizedRGB(0, 0, 0, "Pitch black");
System.out.println(color.getRGB());
}
}
每个分量都是一个字节(8位),一个整数是32位,所以你可以在一个32位的整数中存储4个8位的数字,8位alpha、红、绿、蓝。 >> 或 << 运算符是位移运算符,将数字中的位移动指定的量。 1 << 1 结果为 2。查看二进制序列...
00000001 << 1 = 00000010
左移运算符<< x
相当于乘以2^x,所以可以写成
return ((red * 65536 ) + (green *256) + blue);
我是 Java 的新手。我从 Java Oracle 教程中看到了下面的代码。我很难理解这段代码的用途:
public synchronized int getRGB() {
return ((red << 16) | (green << 8) | blue);
}
我了解按位运算符的工作原理,但我很难理解为什么 getRGB() 方法是这样编写的。是否有其他方法来编写 getRGB() 方法?
public class SynchronizedRGB {
// Values must be between 0 and 255.
private int red;
private int green;
private int blue;
private String name;
private void check(int red,
int green,
int blue) {
if (red < 0 || red > 255
|| green < 0 || green > 255
|| blue < 0 || blue > 255) {
throw new IllegalArgumentException();
}
}
public SynchronizedRGB(int red,
int green,
int blue,
String name) {
check(red, green, blue);
this.red = red;
this.green = green;
this.blue = blue;
this.name = name;
}
public void set(int red,
int green,
int blue,
String name) {
check(red, green, blue);
synchronized (this) {
this.red = red;
this.green = green;
this.blue = blue;
this.name = name;
}
}
public synchronized int getRGB() {
return ((red << 16) | (green << 8) | blue);
}
public synchronized String getName() {
return name;
}
public synchronized void invert() {
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
name = "Inverse of " + name;
}
public static void main(String[] args) {
SynchronizedRGB color = new SynchronizedRGB(0, 0, 0, "Pitch black");
System.out.println(color.getRGB());
}
}
每个分量都是一个字节(8位),一个整数是32位,所以你可以在一个32位的整数中存储4个8位的数字,8位alpha、红、绿、蓝。 >> 或 << 运算符是位移运算符,将数字中的位移动指定的量。 1 << 1 结果为 2。查看二进制序列... 00000001 << 1 = 00000010
左移运算符<< x
相当于乘以2^x,所以可以写成
return ((red * 65536 ) + (green *256) + blue);