Ruby实变量用不着声明.这暗含着对象弹性结构.实际上,每个实变量都是在第次出现时动态加入对象.
ruby> InstTest
| def _foo(n)
| @foo = n
| end
| def _bar(n)
| @bar = n
| end
| end
nil
ruby> i = InstTest.
#<InstTest:0x83678>
ruby> i._foo(2)
2
ruby> i
#<InstTest:0x83678 @foo=2>
ruby> i._bar(4)
4
ruby> i
#<InstTest:0x83678 @foo=2, @bar=4>
注意上例中直到了 _bar思路方法 i 才报告 @bar 值.
最新评论