【问题标题】:How do I specify which constructor of a class I want to call?如何指定要调用的类的哪个构造函数?
【发布时间】:2015-04-19 19:11:41
【问题描述】:

我正在从另一个类创建定义数量的对象,并尝试使用 java.awt.Color 为每个对象随机化颜色。

for (int i = 0; i < numBalls; i++){       
    ballsInSim.add(
        new BoxBall(
            0,
            0,
            (int) boxWidth,
            (int) boxHeight,
            rng.nextInt(35) + 15,
            rng.nextInt(500) + 25,
            rng.nextInt(500) + 25,
            Color.BLUE, // Create new Colour here using constructor
            myCanvas
        )
    );
}

Color.BLUE 当前在哪里,我想调用 Color 的构造函数之一,它使用三个整数作为红色、绿色和蓝色值 (Color(int r, int g, int b))。

如何调用该构造函数?我对 Java 比较陌生,我在解决这个问题时遇到了一些麻烦。

【问题讨论】:

  • 用新颜色(r,g,b) 替换 Color.BLUE。 r、g 和 b 可以是您设置的字段,例如:Random rand = new Random();浮动 r = rand.nextFloat();
  • 很可能就是这样。我忘了输入“新”关键字。菜鸟失误。我将编辑我的代码并发布发生的情况。

标签: java


【解决方案1】:

为了实现你想要的,只需添加以下内容:

new Color(0, 0, 255)

所以本质上是这样的:

ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight, rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25, new Color(0, 0, 255), myCanvas));

为了实现每次颜色随机:

Random R = new Random(256);
Random G = new Random(256);
Random B = new Random(256);

//your color constructor will then be:
new Color(R.nextInt(), G.nextInt(), B.nextInt());

要了解有关颜色类的更多信息,请参阅:Color: Java 7

希望对你有帮助

【讨论】:

  • 不会是Random(256)吗?
  • 我很可能只使用一个 Random 对象并为我想要的每个值调用它。例如:`Random rn = new Random(); int r = rn.nextInt(255);'
  • @TejjD“灵活性”?你的意思是随机数有点随机数?在当前的形式中,代码显然是错误的
  • @soltk 你绝对应该使用nextInt(256)。否则(使用nextInt()),您很快就会收到IllegalArgumentException
【解决方案2】:

如果你用你想要的参数调用构造函数,编译器会选择正确的。

【讨论】:

  • 其实这个定义是错误的stackoverflow.com/questions/12893907/…
  • 我觉得有意思的是,Overloading 不被认为是多态的,但是后面一句话就叫做静态多态。这是一种有趣的看待它的方式。我会更新答案
  • 我已经尝试过这样做。它给了我一个“找不到指定的方法”。我确保我导入了 Color 类并正确编写了构造函数。
  • 可能是您的颜色类别有误。发布显示导入的整段代码可能值得
【解决方案3】:

关于实际问题:Color 有多个构造函数。其中一些仅在参数类型上有所不同,即:

Color(float r, float g, float b)
Color(int r, int g, int b)

当你打电话时

Color c = new Color(r,g,b);

关于将调用哪个构造函数的问题实际上有点棘手:它取决于给定参数的类型。具体可以参考Java语言规范的Section 15.9.3. Choosing the Constructor and its Arguments

但是,在这种情况下,您可以简单一点:如果您传入三个 float 值,则将调用 float 版本。如果传入三个int 值,则将调用int 版本。

关于实现:如果您的意图是创建随机颜色,我建议创建一个小的帮助方法:

private static final Random COLOR_RANDOM = new Random(0);

private static Color createRandomColor() {
    int r = COLOR_RANDOM.nextInt(256);
    int g = COLOR_RANDOM.nextInt(256);
    int b = COLOR_RANDOM.nextInt(256);
    return new Color(r,g,b);
}

然后,你可以简单地在任何地方调用这个方法,例如......

for (int i = 0; i < numBalls; i++)
{       
    ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight,
        rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25, 
        createRandomColor(),  // <---- ... here!
        myCanvas));
}

【讨论】:

    【解决方案4】:

    这很简单,Java 会根据您提供给构造函数的参数来选择要使用的构造函数。如果是颜色,您有 7 个选项,如下所示:

    Color(ColorSpace cspace, float[] components, float alpha)
    

    使用浮点数组中指定的颜色分量和指定的 alpha 在指定的 ColorSpace 中创建颜色。

    Color(float r, float g, float b)
    

    使用范围 (0.0 - 1.0) 内的指定红色、绿色和蓝色值创建不透明的 sRGB 颜色。

    Color(float r, float g, float b, float a)
    

    使用范围 (0.0 - 1.0) 内的指定红色、绿色、蓝色和 alpha 值创建 sRGB 颜色。

    Color(int rgb)
    

    使用指定的组合 RGB 值创建不透明的 sRGB 颜色,该值由 16-23 位中的红色分量、8-15 位中的绿色分量和 0-7 位中的蓝色分量组成.

    Color(int rgba, boolean hasalpha)
    

    创建具有指定组合 RGBA 值的 sRGB 颜色,该值由 24-31 位中的 alpha 分量、16-23 位中的红色分量、8-15 位中的绿色分量和0-7 位中的蓝色分量。

    Color(int r, int g, int b)
    

    使用范围 (0 - 255) 内的指定红色、绿色和蓝色值创建不透明的 sRGB 颜色。

    Color(int r, int g, int b, int a)
    

    创建一个 sRGB 颜色,在 (0 - 255) 范围内具有指定的红色、绿色、蓝色和 alpha 值。

    如果您提供三个 int,则将使用最后一个。如果有三个 浮点数,它将是第三个。

    在您的情况下,您只需将 Colour.BLUE 替换为:

    new Colour(
        new Random().nextInt(256),
        new Random().nextInt(256),
        new Random().nextInt(256),
    )
    

    【讨论】:

      【解决方案5】:

      假设你在this constructor之后,那么

      new Color(r, g, b)
      

      正在使用该构造函数实例化类,其中rgb 是对应于红色、绿色和蓝色的变量/字段。

      【讨论】:

      • 我忘记了“新”关键字。菜鸟失误。但是,我在使用整数而不是浮点小数的构造函数之后。它应该工作相同。
      • @soltk 然后就用那个:docs.oracle.com/javase/7/docs/api/java/awt/…, int, int%29
      【解决方案6】:

      这样做:

      ballsInSim.add(new BoxBall(0, 0, (int) boxWidth, (int) boxHeight, rng.nextInt(35) + 15, rng.nextInt(500) + 25, rng.nextInt(500) + 25, new Color(r, g, b), myCanvas));}
      

      其中r, g, b是之前定义的0到255的随机整数;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2021-09-19
        相关资源
        最近更新 更多