继承和复制类变量

时间:2014-01-01 03:19:38

标签: ruby class oop

这是一个模仿另一个项目中正在发生的事情的人为例子。我确定我只是误解了课程的一些基本功能,但我不知道在哪里寻找答案。

如果我在父类中设置了一个实例变量@empty = [],那么我想使用原始实例变量,以及子类中实例变量的变体,我遇到了一个问题原始实例变量不再以其原始形式访问。

class Parent
  def initialize
    @empty = []
  end

  def fill
    @empty << "foo" #interestingly, changing the << to = fixes it, but that's not a possibility on my project
  end
end

class Child < Parent
  def initialize
    super
    @should_still_be_empty = @empty #trying to "make a copy" of the original
  end

  def show_original_variable
    @should_still_be_empty
  end

end

child = Child.new
child.fill #=> ["foo"], which is what it should return
child.show_original_variable #=> ["foo"], I want it to return []

<小时/>

更新:

在玩了一点之后,我注意到这也发生在同一个类中。为什么呢?

class Child 
  def initialize
    @original_empty = []
    @copy_of_empty = @original_empty
  end

  def fill
    @copy_of_empty << "foo"
  end

  def show_empty
    @original_original_variable
  end

end

child = Child.new
child.fill #=> ["foo"]
child.show_original_variable #=> ["foo"], but I want it to be empty

2 个答案:

答案 0 :(得分:1)

执行@copy = @original将对@copy实例变量中原始对象的引用存储起来。

使用Object#dup将创建您要分配的对象的浅表副本,并将其存储在实例变量中。

class Child 
  def initialize
    @original_empty = []
    @copy_of_empty = @original_empty.dup
  end

  def fill
    @copy_of_empty << "foo"
  end

  def show_empty
    @original_empty
  end

end

child = Child.new
child.fill
#=> ["foo"]
child.show_empty
#=> []

答案 1 :(得分:1)

你的问题与类或继承没有任何关系。相反,它处理与赋值变异相关的更多基本问题。

xs = [11,22,33]
ys = xs           # Both variables hold a reference to the same data.

zs = xs.dup       # But zs has a copy (at least a shallow copy).

xs << 44          # If we *mutate* xs, ys changes also.

p xs              # [11, 22, 33, 44]
p ys              # [11, 22, 33, 44]
p zs              # [11, 22, 33]

xs = [4,5,6]      # If we *assign* to xs, ys won't be affected.

p xs              # [4,5,6]
p ys              # [11, 22, 33, 44]

即使xs数组开始为空(如代码中),也会观察到相同的行为,但使用数据值进行说明会更容易一些。

如果您希望@should_still_be_empty拥有基础数据值的独立副本,则需要按以下方式执行操作:

@should_still_be_empty = @empty.dup