huh*_*cat 9 ruby accessor class-variables
我有一个带有类变量的模块
module Abc
@@variable = "huhu"
def self.get_variable
@@variable
end
class Hello
def hola
puts Abc.get_variable
end
end
end
a = Abc::Hello.new
a.hola
Run Code Online (Sandbox Code Playgroud)
是否可以不使用方法进入@@variable内部?我的意思是说会很好.只是好奇.Helloget_variableAbc.variable
您不能直接访问@@variable(即)模块中类Abc.variable的范围内。为什么?因为,当 Ruby 解释器看到类似 的东西时,它会认为是 Abc 的类/模块方法。HelloAbcAbc.variablevariable
使用 Ruby 编程时,以 Ruby 方式思考非常重要。
| 归档时间: |
|
| 查看次数: |
3438 次 |
| 最近记录: |