让java等待直到调用一个属性?

发布于 2024-12-23 12:15:08 字数 197 浏览 1 评论 0原文

我最近开始使用JOGL,所以我做了一个向量类。在这个类中,我使用了行

    public Vector unit=new Vector(x/length,y/length,z/length);

来查找单位向量。当然,这会导致堆栈溢出。有没有办法让java在运行之前等待单元被调用,或者我必须让单元成为一个方法?

I recently started working with JOGL, so I made a vector class. Inside this class, I used the line

    public Vector unit=new Vector(x/length,y/length,z/length);

To find the unit vector. And of course, this causes a stackoverflow. Is there any way to make java wait for unit to be called before running this or will I have to make unit a method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦醒灬来后我 2024-12-30 12:15:08

我亲自创建了第二个构造函数,它计算单位向量并将其自己的单位向量设置为自身。理想情况下,您应该像 Ernest 建议的那样使用私有值和 get 方法。这样做的原因是,否则其他类可以简单地覆盖 x、y、z 等值(如果它们有权访问您的对象之一)。 Java 具有使用 Final 类进行纯数据存储的传统。例如,请参阅 String 类。您无法修改现有的String,只能创建一个新的String。创建后,String 保持不变。就您的目的而言,这可能并不重要,但在不同的上下文中,如果您的类被不知情的人使用,则可能会导致您的应用程序行为不当。在某些情况下甚至可能存在安全风险。

您可以简单地忽略这一点并直接访问变量,并享受更简洁的代码和较小的性能提升。但我仍然建议了解未来的问题是什么。

不管怎样,下面是我建议的解决单位向量问题的代码,减去 getter 方法。

import java.lang.Math;

class Vector{
    public double x,y,z,length;
    public Vector unit;

    public static void main(String[]s){
        new Vector(5,5,5);

    }

    public Vector(double x, double y, double z){
        this.length = Math.sqrt(x*x + y*y + z*z);

        this.x=x;
        this.y=y;
        this.z=z;

        this.unit = new Vector(x/length, y/length, z/length, true);
    }

    private Vector(double x, double y, double z, boolean isUnitVector){
        // Temp variable for calculating the length
        double length = Math.sqrt(x*x + y*y + z*z);

        if (isUnitVector){
            this.length = 1;

            this.x=x/length;
            this.y=y/length;
            this.z=z/length;

            this.unit = this;
        }else{
            this.length = Math.sqrt(x*x + y*y + z*z);

            this.x=x;
            this.y=y;
            this.z=z;

            this.unit = new Vector(x/length, y/length, z/length, true);
        }

    }

}

我对布尔参数后面的构造函数之间的代码重复并不完全满意。在实践中,我可能会使用一个静态方法创建一个工厂类 VectorFactory,其唯一的工作是创建 Vector 对象。或者也许只使用 Java 自己的 javax.vecmath.Vector3d 和相关类。

I woukld personally create a second constructor, which calculates the unit vector and sets its own unit vector to itself. You should ideally use private values and a get method as Ernest suggests. The reason for this is that otherwise other classes can simply overwrite the x,y,z, etc. values if they have access to one of your objects. Java has a tradition of using final classes for pure data storage. See the String class for example. You can't modify an existing String, only create a new String. Once created, a String remains the same. For your purposes it might not matter much, but in a different context it may cause your application to misbehave, if your class is used by someone who doesn't have a clue. It might even be a security risk in some cases.

You could simply ignore this and access the variables directly, and enjoy the less cluttered code and small performance increase. But I would still suggest knowing what the problem is for the future.

Anyway, below is my suggested code for solving the unit vector problem, minus getter methods.

import java.lang.Math;

class Vector{
    public double x,y,z,length;
    public Vector unit;

    public static void main(String[]s){
        new Vector(5,5,5);

    }

    public Vector(double x, double y, double z){
        this.length = Math.sqrt(x*x + y*y + z*z);

        this.x=x;
        this.y=y;
        this.z=z;

        this.unit = new Vector(x/length, y/length, z/length, true);
    }

    private Vector(double x, double y, double z, boolean isUnitVector){
        // Temp variable for calculating the length
        double length = Math.sqrt(x*x + y*y + z*z);

        if (isUnitVector){
            this.length = 1;

            this.x=x/length;
            this.y=y/length;
            this.z=z/length;

            this.unit = this;
        }else{
            this.length = Math.sqrt(x*x + y*y + z*z);

            this.x=x;
            this.y=y;
            this.z=z;

            this.unit = new Vector(x/length, y/length, z/length, true);
        }

    }

}

I'm not entirely happy with the code duplication between the constructors that follows from the boolean argument. In practice, I would probably create a factory class, VectorFactory with one static method, whose only job is to create Vector objects. Or maybe just use Java's own javax.vecmath.Vector3d and related classes.

情绪失控 2024-12-30 12:15:08

是的,这很简单,但是您需要稍微修改一下您的设计。最重要的是,与所有成员变量几乎总是一样,unit 应该是 private,并且对它的所有访问都应该通过名为 getUnit 之类的方法进行()。然后,您只需编写 getUnit() 来检查 unit 是否已初始化:

public synchronized Vector getUnit() {
    if (unit == null)
        unit = new Vector(x/length,y/length,z/length);
    return unit;
}

我已将此方法设为同步,以便您'如果两个不同的线程几乎同时调用 getUnit() 并且 unit 尚未初始化,将避免出现任何问题。

Yes, this is easy enough, but you'll need to fix your design a bit. Most importantly, as is almost always the case with all member variables, unit should be private, and all access to it should be through a method named something like getUnit(). Then, you simply write getUnit() to check whether unit has been initialized or not:

public synchronized Vector getUnit() {
    if (unit == null)
        unit = new Vector(x/length,y/length,z/length);
    return unit;
}

I've made this method synchronized so that you'll avoid any problems if two different threads call getUnit() at around the same time, and unit hasn't been initialized yet.

剧终人散尽 2024-12-30 12:15:08

我提出了一个构造函数,它可以自行决定它是否是单位向量。如果它是单位向量,则 unit 指向其自身。这将破坏构造函数的递归。
唯一的问题可能是由于舍入错误,length 不完全是 1.0 的数字。

public class Vector {
    public double x, y, z;
    public Vector unit;

    public Vector(double x, double y, double z){
        this.x = x;
        this.y = y;
        this.z = z;
        double length = calcLength(x, y, z);
        if( length == 1.0 )  // perhaps add a little fuzz factor.
            this.unit = this;
        else
            this.unit = new Vector(x/length, y/length, z/length);
    }
}

I propose a constructor which decides itself whether it is a unit vector or not. If it is a unit vector, then unit points to itself. This will break the recursion of the constructor.
The only problem might be numbers where length is not exactly 1.0 due to rounding errors.

public class Vector {
    public double x, y, z;
    public Vector unit;

    public Vector(double x, double y, double z){
        this.x = x;
        this.y = y;
        this.z = z;
        double length = calcLength(x, y, z);
        if( length == 1.0 )  // perhaps add a little fuzz factor.
            this.unit = this;
        else
            this.unit = new Vector(x/length, y/length, z/length);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文