我不确定我是否在这里正确使用了实例变量 – 基于 RGB 的对象(Java)

huangapple go评论79阅读模式
英文:

I'm not sure if I'm using instance variables correctly here - RGB based objects (Java)

问题

我正在努力弄清楚在对象内部使用实例变量的相互作用,但同时能够使用接受其他对象参数的方法来更改对象。该程序应当使用一个带有整型参数的 Color 对象,用于关联颜色的 R、G 和 B 值。

例如:

public void add(Color c) {
    red = red + c.red;
    blue = blue + c.blue;
    green = green + c.green;

    if (red > 255) {
        red = 255;
    }
    if (green > 255) {
        green = 255;
    }
    if (blue > 255) {
        blue = 255;
    }
}

我不知道是否与我尝试使用的格式有关,但我似乎无法弄清楚我到底在哪里出错了。供参考,这是我正在尝试使用的构造函数:

public Color(int r, int g, int b) {
    red = r;
    green = g;
    blue = b;
    if (r > 255) {
        red = 255;
    }
    if (r < 0) {
        red = 0;
    }
    if (g > 255) {
        green = 255;
    }
    if (g < 0) {
        green = 0;
    }
    if (b > 255) {
        blue = 255;
    }
    if (b < 0) {
        blue = 0;
    }
}

这是我运行的测试,试图查看我的方法是否有效:

public final static Color RED = new Color(255, 0, 0);

public static void main(String[] args) {
    Color test = new Color (0, 0, 0);

    test.add(RED);
    System.out.println(test.red + test.green + test.blue);
}

然而,这只产生了 0。任何对我理解问题的帮助或见解都将非常有用!

英文:

I'm struggling to figure out the interactions in using instance variables within objects, but being able to change objects with methods that take other object parameters. The program is supposed to use a Color object with int parameters for the R, G, and B values to associate itself with a color.

For example:

   public void add(Color c) {
        red = red + c.red;
        blue = blue + c.blue;
        green = green + c.green;

        if (red &gt; 255) {
            red = 255;
        }
        if (green &gt; 255) {
            green = 255;
        }
        if (blue &gt; 255) {
            blue = 255;
        }
    }

I don't know if it's an issue with the format I'm attempting to use, but I can't seem to figure out where exactly I'm tripping up. For reference, this is the constructor I'm trying to use:

    public Color(int r, int g, int b) {
        red = r;
        green = g;
        blue = b;
        if (r &gt; 255) {
            red = 255;
        }
        if (r &lt; 0) {
            red = 0;
        }
        if (g &gt; 255) {
            green = 255;
        }
        if (g &lt; 0) {
            green = 0;
        }
        if (b &gt; 255) {
            blue = 255;
        }
        if (b &lt; 0) {
            blue = 0;
        }

    }

This was the test I was running to try to see if my methods were working:

    public final static Color RED = new Color(255, 0, 0);

    public static void main(String[] args) {
        Color test = new Color (0,0,0);

        test.add(RED);
        System.out.println(test.red + test.green + test.blue);
    }

However this just yielded 0. Any help or insight to what I'm not understanding would be wonderful!

答案1

得分: 1

没有代码中明显的问题,但我建议为Color类编写一个toString方法,以及一些getter和setter方法。然而,这应该将其格式化为十六进制字符串(#rrggbb)。

public String toString() {
    return String.format("#%02x%02x%02x", red, green, blue);
}
英文:

There doesn't seem to be any glaring issues with the code, but I would recommend writing a toString method for the Color class along with some getters and setters. However, this should nicely format it as a hex string (#rrggbb).

public String toString() {
    String.format(&quot;#%02x%02x%02x&quot;, red, green, blue)
}

huangapple
  • 本文由 发表于 2020年9月10日 10:33:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63821999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定