简体   繁体   English

Java多构造函数设置问题

[英]Java Multiple Constructor Setting Issue

So I am having issues with the multiple constructors that I am using. 所以我在使用多个构造函数时遇到问题。 Basically, I have two WaterDrop constructs where I pass in the initial x and y position (second block of code) into the appropriate constructor. 基本上,我有两个WaterDrop构造,将初始的x和y位置(第二个代码块)传递到适当的构造函数中。 The issue is that it doesn't set int x, y instance variables to the appropriate starting positions. 问题是它没有将int x,y实例变量设置为适当的起始位置。 It sets them fine the the first constructor but then when it draws the dots, using the second constructor, it automatically sets them to the (0, 0) position. 它在第一个构造函数中将它们设置为精细,但是在使用第二个构造函数绘制点时,它将自动将它们设置为(0,0)位置。 Is there anyway I could call the first constructor so it sets the x and y position to the appropriate starting location? 无论如何,我可以调用第一个构造函数,以便将x和y位置设置为适当的起始位置吗?

public class WaterDrop
{
// instance variables - replace the example below with your own
private int x;
private int y;
private int xVelocity;
private int yVelocity;
private DrawingPanel panel;
private static int DIAMETER = 1;
private int delayStart;
private int bounceLimit;

private static Random rand = new Random();

public WaterDrop(int x, int y){
    this.x = x; //**These assign just fine but I can't get them to get passed**
    this.y = y;  //**into the next WaterDrop constructor**
                 //**i.e. (200, 400)**

}

public WaterDrop(DrawingPanel panel, boolean move)
{
    //initialise instance variables //**In this constructor they are still**
                                    //**initialized to 0**
    this.panel = panel;  //**Since they are initialized at 0, when I draw the**
                         //**waterDrops they appear at the location (0, 0)**

}

    xVelocity = rand.nextInt(3) - 1;
    yVelocity = rand.nextInt(20) + 1 ;
    delayStart = rand.nextInt(100);
    bounceLimit = 0;

}

This is what I'm passing in from the WaterFountain class: 这是我从WaterFountain类中传入的内容:

public class WaterFountain{
....

public WaterFountain(DrawingPanel panel, int xLocation, int yLocation)
{
    this.panel = panel;
    this.xLocation = xLocation;
    this.yLocation = yLocation;

    for(int i = 0; i < NUM_WATER_DROPS; i++){
         waterDrop[i] = new WaterDrop(this.xLocation, this.yLocation);
    }
}


....
}

Your second constructor can't just magically "know" the appropriate values for x and y . 您的第二个构造函数不能仅仅神奇地“知道” xy的适当值。 You have to give it the appropriate values. 您必须为其提供适当的值。 And the only way to do that is to add int x and int y arguments to it. 唯一的方法是向其中添加int xint y参数。 Then, you can set the x and y instance variables either by invoking the first constructor: 然后,可以通过调用第一个构造函数来设置xy实例变量:

public WaterDrop(int x, int y, DrawingPanel panel, boolean move)
{
    this(x, y);  // invoke the WaterDrop(int, int) ctor
    this.panel = panel;  
}

or just set x and y directly: 或者直接设置xy

public WaterDrop(int x, int y, DrawingPanel panel, boolean move)
{
    this.x;
    this.y;
    this.panel = panel;  
}

“使用第二个构造函数绘制点时,它会自动将它们设置为(0,0)位置”,这是因为每次初始化构造函数时都会创建一个新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM