让java等待直到调用一个属性?
我最近开始使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我亲自创建了第二个构造函数,它计算单位向量并将其自己的单位向量设置为自身。理想情况下,您应该像 Ernest 建议的那样使用私有值和 get 方法。这样做的原因是,否则其他类可以简单地覆盖 x、y、z 等值(如果它们有权访问您的对象之一)。 Java 具有使用 Final 类进行纯数据存储的传统。例如,请参阅
String
类。您无法修改现有的String
,只能创建一个新的String
。创建后,String
保持不变。就您的目的而言,这可能并不重要,但在不同的上下文中,如果您的类被不知情的人使用,则可能会导致您的应用程序行为不当。在某些情况下甚至可能存在安全风险。您可以简单地忽略这一点并直接访问变量,并享受更简洁的代码和较小的性能提升。但我仍然建议了解未来的问题是什么。
不管怎样,下面是我建议的解决单位向量问题的代码,减去 getter 方法。
我对布尔参数后面的构造函数之间的代码重复并不完全满意。在实践中,我可能会使用一个静态方法创建一个工厂类
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 existingString
, only create a newString
. Once created, aString
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.
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 ownjavax.vecmath.Vector3d
and related classes.是的,这很简单,但是您需要稍微修改一下您的设计。最重要的是,与所有成员变量几乎总是一样,
unit
应该是 private,并且对它的所有访问都应该通过名为getUnit 之类的方法进行()
。然后,您只需编写getUnit()
来检查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 likegetUnit()
. Then, you simply writegetUnit()
to check whetherunit
has been initialized or not:I've made this method
synchronized
so that you'll avoid any problems if two different threads callgetUnit()
at around the same time, andunit
hasn't been initialized yet.我提出了一个构造函数,它可以自行决定它是否是单位向量。如果它是单位向量,则
unit
指向其自身。这将破坏构造函数的递归。唯一的问题可能是由于舍入错误,
length
不完全是1.0
的数字。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 exactly1.0
due to rounding errors.