I am creating a population genetics simulat开发者_JS百科ion system in JRuby.
I have created classes such as Allele, Gene, Organism, Population.
A Population object contains multiple Organism objects (in an array), each Organism object contains multiple Genes, and so on.
Currently, it is very easy to know, for example, what Gene objects a certain Organism object has. However, if all I have is a Gene object, I do no know what Organism it belongs to.
One solution I have thought of is:
attr_accessor :name, :parent
def initialize(name, parent)
@name = name
@parent = parent #parent will be the parent object
end
If I do this, will @parent merely be a reference to the parent object in memory, or will it be a whole new object?
Is this the best way to do this? Is there another 'better' way of doing this?
Thanks!
JRuby will, as you suggest, just keep a reference to the object rather than creating a copy of it.
This is a perfectly sensible way to approach the problem. You are keeping a reference to an object you want to refer to!
精彩评论