UPDATE
public Fish mate(Fish other){ if (this.health > 0 && other.health > 0 && this.closeEnough(other)){ int babySize = (((this.size + other.size) /2)); int babyHealth = (((this.health + other.health) /2)); double babyX = (((this.x + other.x) /2.0)); double babyY = (((this.y + other.y) /2.0)); new Fish (babySize,babyHealth,babyX,babyY); } return null; }
当调用新的Fish时,是否有一个新的Fish实例在没有引用的地方浮动,或者我是否只为新Fish分配内存而没有实际实例化它?
解决方法
When new @H_502_14@Fish is called,is there a new instance of Fish floating around somewhere without a variable name or have I just allocated memory for a new @H_502_14@Fish without actually instantiating it?
@H_502_19@将创建一个新的Fish对象,并且将进行垃圾收集,因为没有对它的引用.
垃圾收集将在Fish的构造函数完成后(某个时间)进行.
在你的情况下,没有多大意义,但有时它确实如此,如果实例化一个对象将启动一个新的线程或运行一些你想要只运行一次的其他例程.If I have only allocated memory or there is a @H_502_14@Fish without a name,how can I get the new @H_502_14@Fish call to create an actual instance of the @H_502_14@Fish with a unique variable name?
@H_502_19@这不是很清楚.但我觉得你只想回归新鱼(…);并在你调用它的地方将它分配给一个变量,例如:
Fish babyFish = femaleFish.mate(maleFish);