简体   繁体   English

Java - 在没有方法的情况下编辑实例变量

[英]Java - Edit instance variables without a method

I am quite new to Java, and I am constantly looking for ways to improve my code. 我对Java还是很陌生,并且我一直在寻找改善代码的方法。 But I don't seem to get this, if it is even possible to do. 但是,即使有可能,我似乎也没有得到。

Let's say I have this code (I edited out the irrelevant parts, so the code might seem weird): 假设我有这个代码(我编辑了不相关的部分,所以代码可能看起来很奇怪):

public class NewBody {

public static int distanceScale = 5;

    public int x, y;
    public float xMeter = x * distanceScale;
    public float yMeter = y * distanceScale;

    public NewBody(int x, int y){
        this.x = x;
        this.y = y;
    }

    public void pixToMeter(){
         this.xMeter = distanceScale * this.x;
    }

If I don't call pixToMeter() and just try to use "instance.xMeter" directly, it just returns the vaulue 0, even though I've already set the x variable in the constructor. 如果我不调用pixToMeter()并尝试直接使用“instance.xMeter”,它只返回vululue 0,即使我已经在构造函数中设置了x变量。

So my question is: Is there a way to properly set variables without calling a method to do it? 所以我的问题是:有没有办法在不调用方法的情况下正确设置变量? It seems highly unneccessary since I am not even passing a parameter to it. 这似乎是非常不必要的,因为我甚至没有传递参数。

Sorry for my poor english, I hope you understand what I am trying to say. 对不起,我的英语不好,希望您能理解我的意思。

The initialisation of xMeter is done when x is still zero. 当x仍然为零时,xMeter的初始化完成。

This is what actually happens: 这是实际发生的事情:

public NewBody(int x, int y) {
    // All fields are zeroed: 0, null, 0.0.

    super(); // Object constructor, as Object is the parent class.

    // Those fields that are initialized:
    xMeter = this.x * distanceScale; // 0.0f * 5
    yMeter = this.y * distanceScale;

    // The rest of the constructor:
    this.x = x;
    this.y = y;
}

For a depending value: 对于依赖值:

public final void setX(int x) {
    this.x = x;
    xMeter = this.x * distanceScale;
}

And to apply the DRY principle (Don't Repeat Yourself): one could drop the initialisation of xMeter and call setX(x) in the constructor instead. 并应用DRY原理(不要重复自己):可以删除xMeter的初始化,然后在构造函数中调用setX(x)。

When called in the constructor it is important to make setX final, that is: not overridable. 在构造函数中调用时,使setX final很重要,即:不可重写。

The source of the problem is here: 问题的根源在这里:

public float xMeter = x * distanceScale;

The issue is that you're initializing this instance variable outside the constructor. 问题是您要在构造函数外部初始化此实例变量。 As a result, since x is initialized to 0, the result of your multiplication is also 0. 结果,由于x被初始化为0,因此乘法的结果也是0。

If you need xMeter and yMeter initialized to a value based on x or y , simply declare them as you did the other fields: 如果需要将xMeteryMeter初始化为基于xy的值,则只需像其他字段一样声明它们即可:

public int xMeter;

And initialize their values in the constructor: 并在构造函数中初始化它们的值:

public newBody(int x, int y){
    // initialize x and y ...
    this.xMeter = x * distanceScale;

As others have mentioned, when the xMeter is initialized, the constructor is not called yet and x is still 0 , so the value of xMeter is 0 as well. 正如其他人所提到的,当初始化xMeter时,构造函数尚未被调用且x仍为0 ,因此xMeter的值xMeter0

To change that, you must update xMeter 's value once x is initialized in the constructor, like so: 要更改它,必须在构造函数中初始化x更新xMeter的值,如下所示:

public NewBody(int x, int y){
    this.x = x;
    this.y = y;

    // update x and y meter
    xMeter = x * distanceScale;
    yMeter = y * distanceScale;
}

However, you mentioned how you want xMeter to update every time x is changed as well. 但是,您提到了如何让xMeter在每次x更改时也进行更新。 As it stands with your current code, that will not happen. 因为它与您当前的代码一致,所以不会发生这种情况。 However, a suggestion of mine would be to create a method to change the value of x (and y as well) and in those methods, also update the values of xMeter and yMeter . 但是,我的建议是创建一个方法来改变x (和y )的值,在这些方法中,还要更新xMeteryMeter的值。 That way, whenever you want to change x , call the methods and it will update your other values too. 这样,无论何时想要更改x ,都要调用方法,它也会更新其他值。

Try adding these methods and changing your constructor to this: 尝试添加以下方法,并将构造方法更改为此:

// called setter methods
public void setX(int x) {
    this.x = x;
    this.xMeter = x * distanceScale;
}
public void setY(int y) {
    this.y = y;
    this.yMeter = y * distanceScale;
}

// constructor
public NewBody(int x, int y){
    setX(x);
    setY(y);
}

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

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