Instance Variables Inheritance
有人能解释一个类如何访问它的超类的实例变量,以及它是如何不是继承的吗?我说的是"Ruby编程语言"和示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Point def initialize(x,y) # Initialize method @x,@y = x, y # Sets initial values for instance variables end end class Point3D < Point def initialize(x,y,z) super(x,y) @z = z end def to_s "(#@x, #@y, #@z)" # Variables @x and @y inherited? end end Point3D.new(1,2,3).to_s =>"(1, 2, 3)" |
如果不是继承,那么类
"The reason that they sometimes appear to be inherited is that instance variables are created by the methods that first assign values to them, and those methods are often inherited or chained."
但我不明白这到底是什么意思。
You are right,the book is wrong,or at least poorly worded(P)I would argue that the book i s simply wrong,or at best,it's making a quite muddy explanation.(p)(P)在任何一种语言中,超级阶级和派生阶级都没有分开的目标。当你创造了一个瞬间的中产阶级,它也是一个瞬间的超级阶级。There is one object and it is both classes at 11.(p)(P)因为只有一个目标,所以只有一组变数。(p)(P)This is the same as all other oo systems.The weird argument that book makes about how it just matters which method is run and how the methods themselves are what are really inherited does not add much in the way of clarity.(p)(P)The problem with the terminology is that,sure,in a dynamically typed system there is no declaration in the first place,and so certainly the definition of the subclass doesn't inherit any field declarations…因为没有什么是正常的。但是,仅仅因为存在着不确定因素,就不能使人有机会发表声明("瞬息万变是不被禁止的"),任何更真实的情况都是如此,而且由于父母可能有不同的瞬息万变,因此也就意味着父母在试图就其目标进行讨论时没有意识到结果。(p)
(P)EDOCX1 0极致的呼叫基地阶级的建设者,这是最初的方法。如果你拿了EDOCX1的音标0,他们的变量EDOCX1的音标2和EDOCX1的音标3(p)
(P)这是混乱的工作。@x@y,and@z are all instance variables on that point3d instance.如果超级(x,y)wasn't there,the point3d instance would not have a@x or@y.(p)
(P)你可以把他们比作两个考试。(p)(P)Example 1:It seems b inherit EDOCX1 English 4 from A.(p)字母名称(P)例2:如果B has its own EDOCX1 commonline 5,it cannot find EDOCX1.(p)字母名称(P)EDOCX1 7's EDOCX1(英文)4/in example 1,is actually created implicitly by EDOCX1(英文)9 when you invoke EDOCX1(英文)10.(p)(P)在你的案例中,EDOCX1(p)