Ruby类实例变量与类变量

Ruby类实例变量与类变量

我读过“ Ruby实例变量什么时候设置? ”但我在使用类实例变量时有两种想法。

类变量由类的所有对象共享,实例变量属于一个对象。如果我们有类变量,那么使用类实例变量的余地就不大了。

有人可以解释这两者之间的区别以及何时使用它们?

这是一个代码示例:

class S  @@k = 23
  @s = 15
  def self.s    @s
  end
  def self.k     @@k
  endendp S.s #15p S.k #23

我现在明白了,Class Instance Variables不会传递给继承链!


慕尼黑的夜晚无繁华
浏览 542回答 3
3回答

慕慕森

类上的实例变量:class Parent&nbsp; @things = []&nbsp; def self.things&nbsp; &nbsp; @things&nbsp; end&nbsp; def things&nbsp; &nbsp; self.class.things&nbsp; endendclass Child < Parent&nbsp; @things = []endParent.things << :carChild.things&nbsp; << :dollmom = Parent.newdad = Parent.newp Parent.things #=> [:car]p Child.things&nbsp; #=> [:doll]p mom.things&nbsp; &nbsp; #=> [:car]p dad.things&nbsp; &nbsp; #=> [:car]类变量:class Parent&nbsp; @@things = []&nbsp; def self.things&nbsp; &nbsp; @@things&nbsp; end&nbsp; def things&nbsp; &nbsp; @@things&nbsp; endendclass Child < ParentendParent.things << :carChild.things&nbsp; << :dollp Parent.things #=> [:car,:doll]p Child.things&nbsp; #=> [:car,:doll]mom = Parent.newdad = Parent.newson1 = Child.newson2 = Child.newdaughter = Child.new[ mom, dad, son1, son2, daughter ].each{ |person| p person.things }#=> [:car, :doll]#=> [:car, :doll]#=> [:car, :doll]#=> [:car, :doll]#=> [:car, :doll]使用类上的实例变量(不在该类的实例上),您可以存储该类的公共内容,而不会自动获取子类(反之亦然)。使用类变量,您可以方便地不必self.class从实例对象写入,并且(在需要时)您还可以在整个类层次结构中自动共享。将这些合并到一个示例中,该示例还包含实例上的实例变量:class Parent&nbsp; @@family_things = []&nbsp; &nbsp; # Shared between class and subclasses&nbsp; @shared_things&nbsp; = []&nbsp; &nbsp; # Specific to this class&nbsp; def self.family_things&nbsp; &nbsp; @@family_things&nbsp; end&nbsp; def self.shared_things&nbsp; &nbsp; @shared_things&nbsp; end&nbsp; attr_accessor :my_things&nbsp; def initialize&nbsp; &nbsp; @my_things = []&nbsp; &nbsp; &nbsp; &nbsp;# Just for me&nbsp; end&nbsp; def family_things&nbsp; &nbsp; self.class.family_things&nbsp; end&nbsp; def shared_things&nbsp; &nbsp; self.class.shared_things&nbsp; endendclass Child < Parent&nbsp; @shared_things = []end然后在行动:mama = Parent.newpapa = Parent.newjoey = Child.newsuzy = Child.newParent.family_things << :housepapa.family_things&nbsp; &nbsp;<< :vacuummama.shared_things&nbsp; &nbsp;<< :carpapa.shared_things&nbsp; &nbsp;<< :blenderpapa.my_things&nbsp; &nbsp; &nbsp; &nbsp;<< :quadcopterjoey.my_things&nbsp; &nbsp; &nbsp; &nbsp;<< :bikesuzy.my_things&nbsp; &nbsp; &nbsp; &nbsp;<< :dolljoey.shared_things&nbsp; &nbsp;<< :puzzlesuzy.shared_things&nbsp; &nbsp;<< :blocksp Parent.family_things #=> [:house, :vacuum]p Child.family_things&nbsp; #=> [:house, :vacuum]p papa.family_things&nbsp; &nbsp;#=> [:house, :vacuum]p mama.family_things&nbsp; &nbsp;#=> [:house, :vacuum]p joey.family_things&nbsp; &nbsp;#=> [:house, :vacuum]p suzy.family_things&nbsp; &nbsp;#=> [:house, :vacuum]p Parent.shared_things #=> [:car, :blender]p papa.shared_things&nbsp; &nbsp;#=> [:car, :blender]p mama.shared_things&nbsp; &nbsp;#=> [:car, :blender]p Child.shared_things&nbsp; #=> [:puzzle, :blocks]&nbsp;&nbsp;p joey.shared_things&nbsp; &nbsp;#=> [:puzzle, :blocks]p suzy.shared_things&nbsp; &nbsp;#=> [:puzzle, :blocks]p papa.my_things&nbsp; &nbsp; &nbsp; &nbsp;#=> [:quadcopter]p mama.my_things&nbsp; &nbsp; &nbsp; &nbsp;#=> []p joey.my_things&nbsp; &nbsp; &nbsp; &nbsp;#=> [:bike]p suzy.my_things&nbsp; &nbsp; &nbsp; &nbsp;#=> [:doll]&nbsp;

拉莫斯之舞

我相信主要(只有?)不同的是继承:class T < Sendp T.k=> 23S.k = 24p T.k=> 24p T.s=> nil类变量由所有“类实例”(即子类)共享,而类实例变量仅特定于该类。但如果你从不打算扩展你的课程,那么差异纯粹是学术性的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Ruby