Nat*_*ong 6 ruby garbage-collection
在Ruby中,一个对象可以破坏另一个吗?
例如:
class Creature
def initialize
@energy = 1
end
attr_accessor :energy
end
class Crocodile < Creature
def eat(creature)
@energy += creature.energy
creature = nil #this does not work
end
end
fish = Creature.new
croc = Crocodile.new
croc.eat(fish)
Run Code Online (Sandbox Code Playgroud)
在鳄鱼吃掉一个生物并吸收其能量后,该生物应该不再存在.但是上面的代码并没有破坏这个生物.
我知道,如果我说fish = nil,变量fish引用的对象将被垃圾收集.但是creature = nil在Crocodile的eat方法中说不能做到这一点.
从croc.eat里面,我可以说"因为变量'鱼'被传递给我,当我完成后,我要把'鱼'设置为零?"
我基本上采取了Chuck建议的方法,并进行了一些修改.这是我的推理:
因此,我这样做了:
@creaturelist.(我使用实例变量而不是类变量的原因是任何子类都Creature可以拥有自己的列表.)@creaturelist返回一个ID.@id变量中记住该ID .Creature.remove(@id),并且唯一的引用将被删除.现在我可以这样做:
class Predator < Creature
def eat(creature)
@energy += creature.energy
creature.die
end
end
fish = Creature.new
Creature.list #shows the fish
croc = Predator.new
croc.eat(fish)
Creature.list #no more fish
Run Code Online (Sandbox Code Playgroud)
当然,在这个例子中,fish仍然指向该生物对象,因此它不是垃圾收集的.但最终,生物会根据规则被创造并互相吃掉,所以我不会单独命名它们.
我认为问题在于你认为程序本身就是这些模拟事物生存的世界,而不是模拟它.
fish = Creature.new
croc = Crocodile.new
$world = [fish, croc]
class Crocodile
def eat(creature)
@energy += creature.energy
$world.delete creature
end
end
croc.eat fish
world # [croc], now all by his lonesome, the last creature in the world :(
Run Code Online (Sandbox Code Playgroud)
并且假设fish变量已经超出范围,就像在正确结构化的程序中一样,该对象现在很可能是垃圾.
| 归档时间: |
|
| 查看次数: |
3253 次 |
| 最近记录: |