Ruby 中的@@ 和@ 有什么区别? [英] What is the difference between @@ and @ in Ruby?
本文介绍了Ruby 中的@@ 和@ 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始学习 Ruby,我一直无法很好地解释 @@ 和 @ 在类变量方面的区别.如果有人能提供一个基本的直观示例,那就太好了.它们也可以互换吗?
I just started learning Ruby and I have been unable to find a good explanation on what is the difference between @@ and @ in terms of class variables. If anyone can provide a basic intuitive example, that would be really great. Also are they interchangeable?
推荐答案
以@@
为前缀的变量为类变量,以@
为前缀的为实例变量.在这个答案中可以找到一个很好的描述:https://stackoverflow.com/a/5890199/1181886
A variable prefixed with @@
is a class variable and one prefixed with @
is an instance variable. A great description can be found in this answer: https://stackoverflow.com/a/5890199/1181886
这篇关于Ruby 中的@@ 和@ 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文