我正在为Color类中的对象编写访问器。我想返回与该对象关联的红色,绿色和蓝色值的总和。有没有办法让我调用我创建的对象的参数?如何从Java对象中调用RBG参数
这是我到目前为止已经无济于事试图....
// Sample //
int red = 200;
int green = 51;
int blue = 76;
Color c = new Color(red, green, blue);
System.out.println(c.red);
System.out.println(c.green);
System.out.println(c.blue);
// The above gives a compile error //
请出示'Color'源 –
这是颜色C =新的色彩(红,绿,蓝)或本 的System.out.println(c.red);导致问题? – JFPicard
请注意'c.red'不会**指向颜色对象的内部红色值。它宁可指向表示全红色*(255,0,0)*的常量“颜色”对象。该变量是** static **,因此它应该由'Color.red'而不是'c.red'引用,因为它不是'c'对象的成员,而是'Color'类。如果你想访问c对象的红色属性,那么你应该使用'c.getRed()'。就像一个注释,'Color'还为所有类型的颜色定义了其他常量,例如'Color.BLACK'或'Color.LIGHTBLUE'。 – Zabuza