簡體   English   中英

Eclipse 調試模式改變變量

[英]Eclipse debug mode changing variables

當使用 Eclipse IDE 在 Java 中編程時,我有時會使用調試 function。很多時候我喜歡在程序運行時實時更改變量。 然而,我經常發現改變一些變量實際上不會影響當前運行的程序。

我的問題是:是否有一定的調試規則? scope 中哪些變量是調試器之類的?

如果這是一個愚蠢的問題,我很抱歉。 我對 Eclipse 中的調試和一般編程還很陌生。

下面的代碼是一個示例。 如果它難以閱讀或其他什么,我很抱歉,但問題是:在 Ball class 中,每當我更改最終變量(如PARTICLES_PER_CLICKSPEED )時,它們都會實時更新,我可以看到程序 window 中的差異,但是,當我更改RADIUS變量時,它什么也不做,即使它與其他兩個變量位於相同的 class 中。

public class Main {

    public static final int WIDTH = 1280;
    public static final int HEIGHT = 720;
    public static Ball[] ball = new Ball[100000];
    public Main() {
        try {
            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
            Display.create();
            Display.setTitle("Game Engine");

        } catch (LWJGLException e) {

            e.printStackTrace();
        }

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);


        boolean[] doOnce = new boolean[10];
        boolean gravity = false;
        while (!Display.isCloseRequested()) {

            if (Mouse.isButtonDown(1) || Mouse.isButtonDown(1)) {
                if (!doOnce[0]) {
                    Ball.createParticles();
                    doOnce[0]=true;
                }

            } else {
                doOnce[0] = false;
            }

            glClear(GL_COLOR_BUFFER_BIT);

            for (int i = 1; i <= Ball.ballAmount; i++) {
                ball[i].updatePosition(gravity);
                if (Mouse.isButtonDown(0)) {
                    gravity = true;
                } else {
                    gravity = false;
                }

                if (ball[i].position.x > 0 && ball[i].position.y > 0 && ball[i].position.x < WIDTH && ball[i].position.y < HEIGHT) {
                    glBegin(GL_TRIANGLE_FAN);
                    glVertex2d(ball[i].position.x, ball[i].position.y);
                    for(int u=0;u<=360;u+=5){
                        glVertex2d(ball[i].position.x+Math.cos(u)*Ball.RADIUS, ball[i].position.y+Math.sin(u)*Ball.RADIUS);
                    }
                    glEnd();
                }

            }
            System.out.println("Particle Amount: " + Ball.ballAmount);

            Display.update();
            Display.sync(60);
        }
        Display.destroy();
        System.exit(0);
    }

    public static void main(String[] args) {
        new Main();
    }
}

class Ball {

    public Vector2f position, velocity;
    public static final int RADIUS = 10;
    public static final int INITIAL_SPEED = 5;
    public static final int SPEED = 2;
    public static final int PARTICLES_PER_CLICK = 50;

    public static int ballAmount = 0;

    public double r, g, b;

    public static Random rnd = new Random();

    public Ball(double x, double y) {
        int angle = rnd.nextInt(360);
        position = new Vector2f((float) x, (float) y);
        velocity = new Vector2f((float) Math.cos(angle) * rnd.nextFloat() * INITIAL_SPEED, (float) Math.sin(angle) * rnd.nextFloat() * INITIAL_SPEED);

        this.r = rnd.nextDouble();
        this.g = rnd.nextDouble();
        this.b = rnd.nextDouble();
    }

    public void updatePosition(boolean gravity) {
        this.position.x += this.velocity.x * SPEED;
        this.position.y += this.velocity.y * SPEED;
        if (gravity) {
            double dx = this.position.x - Mouse.getX();
            double dy = this.position.y - (Main.HEIGHT - Mouse.getY());
            double distance = Math.sqrt(dx * dx + dy * dy);

            this.velocity.x -= (this.position.x - Mouse.getX()) / distance;
            this.velocity.y -= (this.position.y - (Main.HEIGHT - Mouse.getY())) / distance;
        } else {
            this.velocity.x *= 0.99;
            this.velocity.y *= 0.99;
        }

    }

    public static void createParticles() {
        for (int i = 1; i <= PARTICLES_PER_CLICK; i++) {

            ballAmount += 1;
            Main.ball[ballAmount] = new Ball(Mouse.getX(),Main.HEIGHT- Mouse.getY());
        }
    }

}

如果優化器看到一個最終變量,它將知道它的值不會改變。 這些知識的作用取決於它。 它可能無能為力(就像在您的情況下似乎發生在PARTICLES_PER_CLICK或SPEED上一樣),或者它可能只是用所有地方的實際值替換了該變量的所有出現。 除了沒有更改最終變量的值之外,沒有特殊的規則。

沒有特殊規則,除了不改變final變量的值。

實際上,Eclipse 4.23(2022 年第二季度,七年后)現在更清楚了:

關於更改最終字段的警告

由於 Eclipse 3.1 Eclipse Java 調試器允許更改最終字段值。

雖然在技術上可行,但此類更改的后果並非微不足道,可能會影響看似無關的代碼並導致各種危險后果。

因此,使用 Eclipse 4.23 Java 調試器現在顯示一個新警告:

調試器警告——https://www.eclipse.org/eclipse/news/4.23/images/final_modification_warning.png

默認情況下啟用此警告,可以通過首選項禁用:

更新首選項 - https://www.eclipse.org/eclipse/news/4.23/images/final_modification_option.png

此外,“ org.eclipse.debug.ui.variableValueEditors ”擴展點已更新,以允許自定義產品為現有調試模型貢獻自己的“ variableValueEditor ”實現,並對final字段修改有更多控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM