类实例问题,Android
我正在尝试制作一款安卓游戏 我的主类中有一个位图和画布实例。
我有另一个渲染器类的实例。 该渲染器类位于同一个包中,但不是我的主类的子类。
如果我将位图和画布实例传递给该渲染器类的方法,并且该方法会将传递的位图绘制到传递的画布,那么是传递实际实例还是创建然后传递新实例副本?好吧,我已经尝试过并看到实际实例正在被传递。因为我看到位图被绘制到画布上。
这是我的问题,为什么实际实例会被传递? 如果是这样的->
public class instanceTest
{
static int num;
static void numIncrementor(int number)
{
number++;
}
public static void main(String[] args)
{
num = 0;
numIncrementor(num);
System.out.println(num);
}
}
在这里,当我打印 num 时,我仍然会得到 0,但对于其他位图和画布,我会发送实际实例。这让我真的很困惑。有人可以解释一下吗? 或者与原始类型不同的类对象总是如此?它会让垃圾收集器发疯吗?
预先感谢,如果您不明白我的意思,请告诉我,我将在这里放置伪代码以供澄清;
I am trying to make a game for android
I have a bitmap and canvas instance in my main class.
I have another instance of, lets say, renderer class.
That renderer class is in the same package, but not the subclass of my main class.
If i pass the bitmap and canvas instance to a method of that renderer class and that method will draw that passed bitmap to the passed canvas, are the actual instances passed or new instance copies created and then passed ? Well, i have tried and saw that actual instances were being passed. Because i was seeing the bitmap being drawn to the canvas.
Here is my question, why are the actual instances are passed ?
if it was something like this ->
public class instanceTest
{
static int num;
static void numIncrementor(int number)
{
number++;
}
public static void main(String[] args)
{
num = 0;
numIncrementor(num);
System.out.println(num);
}
}
Here, when i print the num, i will still get 0, but with other bitmap and canvas thing, i do send the actual instances. This got me really confused. Can someone explain it ?
Or is it always the case with class objects unlike primitive types ? Does it make garbage collector go crazy ?
Thanks in advance, if you did not understand my engrish, tell and i will put pseudo codes here for clarification;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在开始编写 Java 代码之前,您确实应该阅读 Java 教程。
在上面的示例中,您传递了一个原语 - 因此它的值只是传递给方法,放置在堆栈上,并且该值被更改。这就是原语在 Java 中的工作原理。这意味着您的
number
变量仅存在于numIncrementor
方法的范围内 - 并且不会影响外部的内容。您的实际代码适用于对象。当您传递一个对象时,您实际上传递了堆上对该对象的引用。因此,您对其所做的每项更改都将在实际对象上完成。唯一的例外是,您不能将对象“重新分配”到其他对象,因为您只持有对堆的引用 - 您可以将引用“重新分配”到堆上的另一个位置,但原始引用将继续指向原始对象。
至于你关于垃圾收集器的问题 - 不,它不会发疯。它实际上效果很好。 (GC 的类型不止一种,但这是一个完全不同的讨论)。
Java 不使用
复制构造函数
- 如果您想将对象的副本传递给方法,您应该使其可克隆
并传递克隆
> 其中。但这可能不是您所需要的 - 请注意,复制构造函数比仅传递对象的引用消耗更多的资源。You should really read the Java tutorial before starting to write Java code.
In your example above you passed a primitive - so it's value is simply passed to the method, placed on the stack and that value is altered. That is how primitives work in Java. This means that your
number
variable only exists within the scope of thenumIncrementor
method - and does not effect what is outside.Your actual code works with objects. When you pass an object you actually pass the reference to it on the heap. So every change you do to it will be done on the actual object. The only exception is that you cannot "re-assign" the object to something else since you only hold a reference to the heap - you can "re-assign" your reference to another location on the heap, but the original reference will continue to point to the original object.
As for your question on the garbage collector - no, it will not go crazy. It actually works pretty good. (There is more than one type of GC, but that is a whole different discussion).
Java does not use
Copy Constructors
- if you want to pass a copy of an object to a method, you should make itcloneable
and pass aclone
of it. But this is probably not what you need - note thatCopy Constructors
consume more resources than just passing the reference to the object.