Java初始化对象
好吧,假设我有一个类
class Ghost {
private int x, y, direction;
private Color color;
,现在假设我没有显式定义构造函数,所以它只使用默认的构造函数。如果我要创建一个新的 Ghost 对象,direction 的值是什么? 0 对吗?但我不知道为什么我的方向值一直为 1。您认为有什么可能是错误的吗?
Ok so say I have a class
class Ghost {
private int x, y, direction;
private Color color;
Now say I dont explicitly define a constructor, so it just uses the default one. If I were to create a new Ghost object, what value would direction have? 0 right? But I dont know why I keep getting a value of 1 for direction. Anything off the bat that you think could be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能给我们展示更多代码吗?我认为这里还发生了其他事情。
int
变量在 Java 中默认初始化为 0,如果我使用你的代码片段,并实例化一个Ghost
,它的所有int
成员都是 0。因此,根据您的评论,我认为您的代码可能存在问题,例如您在使用
Ghost.setX
或Ghost.setY
direction代码>.在我极其简单的类实现中,此测试代码完全按照预期工作。
自代码发布以来已进行编辑
好的,我有一个基于您的代码的课程。我没有发现其他函数有什么问题,因为很少有类真正改变
方向
。我删除了这些额外的函数,因为除了将其设置为 0 的一件事之外,它们没有修改方向。您应该尝试一个非常简化的测试用例来查看您的代码是否有效。试试这个:
这里一切仍然有效,所以请按原样尝试该代码。这纯粹是您的代码,如果它有效(并且应该有效),那么还有其他代码(例如您的类的用户)正在执行某些操作。
独立地,您的代码应该可以正常工作。
使用调试器
您熟悉调试器吗?如果您使用的是 Eclipse 或 NetBeans,那么您应该习惯于使用集成到其环境中的调试器。这确实可以帮助您通过在方向上放置手表来跟踪方向何时更改值。
使用调试器远远超出了本答案的范围;这是你真正需要阅读并亲自尝试的东西。
以下是使用 Eclipse 进行调试的链接:
Can you show us more code? Methinks something else is going on here.
int
variables will default initialize to 0 in Java, and if I use your snippet, and instantiate aGhost
, all itsint
members are 0.So from your comments, I think there might be an issue with your code, like you are accidentally setting
direction
when usingGhost.setX
orGhost.setY
.In my extremely simple class implementation, this testing code works exactly as intended.
Edited since code was posted
Okay so I have a class based on your code. I don't see anything wrong with the other functions, since very few classes actually alter
direction
. I removed those extra functions because they didn't modify direction except for one thing that would set it to 0.You should try a very reduced test case to see if your code works. Try this:
Everything still works here, so try that code as is. This is purely your code, and if it works (and it should), then there's other code like a user of your class that is doing something.
Standalone, your code should work just fine.
Use the Debugger
Are you familiar with a debugger? If you are using Eclipse or NetBeans, then you should get friendly with using the debuggers integrated into their environments. This should really help you track down when
direction
changes values by placing a watch on it.Using a debugger is way beyond the scope of this answer; it's something you really have to read up on and try yourself.
Here's a link for debugging with Eclipse:
有些东西你没有向我们展示。你的 getDirection() 方法只是返回方向,还是做一些更奇特的事情?是否有一个 setter 方法会做一些奇怪的事情?您没有向我们展示的课程的其他部分是否以某种方式改变了方向?
在Java中,int类型的实例变量被初始化为0,所以你要么没有向我们展示一些东西,要么你正在使用一些非标准的实现(几乎肯定不是后者)。
There's something you aren't showing us. Does your getDirection() method simply return direction, or does it do something more exotic? Is there a setter method that does something strange? Does some other part of the class that you did not show us modify direction in some way?
In Java, instance variables of type int are initialized to 0, so you are either not showing us something or you are using some non-standard implementation (almost definitely not the latter).