我正在爲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