如何引用全局变量和类变量? [英] How to reference global variables and class variables?

查看:67
本文介绍了如何引用全局变量和类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.现在我正在学习Ruby.据我了解,全局变量是在全局命名空间中定义的(因此在任何类或函数之外).我正在阅读一些东西,它说全局变量前面有一个 $ 符号.这意味着什么?这是否意味着当我定义一个函数或类并想要引用我的全局变量(假设它是 edmund = 123)时,我必须像这样引用它:$edmund?

I'm new to programming. Right now I'm studying Ruby. To my understanding, global variables are defined in the global namespace (so outside of any classes or functions). I'm reading something and it says global variables have a $ sign before them. What does that mean? Does it mean when I define a function or class and want to reference my global variable (let's say it is edmund = 123) I would have to reference it like this: $edmund?

所以:

edmund = 123
def my_function()
  456 + $edmund
end

还有类变量(以@@开头的那些)和实例变量(@)一样,你可以通过Class调用它们来访问它们.类变量?他们的目的是什么?

Also are class variables (the ones that begin with @@) like instance variables (@) where you can access them by calling them through Class.classvariable? What is their purpose?

推荐答案

全局范围是覆盖整个程序的范围.全局变量享有全局范围,可通过其初始美元符号 ($) 字符识别.它们随处可用,创建自己的全局变量可能很诱人,尤其是对于初学者来说.但它们并不总是一个好主意.

Global scope is scope that covers the entire program. Global scope is enjoyed by global variables, which are recognizable by their initial dollar-sign ($) character. They’re available everywhere and creating your own global variables can be tempting, especially for beginning programmers. But they’re not always a good idea.

$gvar = "I'm a global!"
class C
    def examine_global
        puts $gvar
    end
end

c = C.new
c.examine_global # I'm a global!

<小时>

类变量以两个 at 符号开头:例如,@@var.尽管它们的名字,类变量不是类范围的.相反,它们是类层次结构范围的.简而言之,类变量背后的想法是它提供了一种在类和该类的实例之间共享的存储机制,并且对任何其他对象都不可见.


Class variables begin with two at signs: @@var, for example. Despite their name, class variables aren’t class scoped. Rather, they’re class-hierarchy scoped. At its simplest, the idea behind a class variable is that it provides a storage mechanism that’s shared between a class and instances of that class, and that’s not visible to any other objects.

class Parent
    @@value = 100
end

class Child < Parent
    @@value = 200
end

class Parent
    puts @@value
end

打印的是 200.Child 类是 Parent 的子类,这意味着 Parent 和 Child 共享相同的类变量——不是同名的不同类变量,而是相同的实际变量.当您在 Child 中分配 @@value 时,您正在设置一个且唯一的 @@value 变量,该变量在整个层次结构中共享-也就是说,由 Parent 和 Child 以及它们中任何一个的任何其他后代类.

What gets printed is 200. The Child class is a subclass of Parent, and that means Parent and Child share the same class variables—not different class variables with the same names, but the same actual variables. When you assign to @@value in Child, you’re setting the one and only @@value variable that’s shared throughout the hierarchy— that is, by Parent and Child and any other descendant classes of either of them.

并给予应有的荣誉 - 此解释来自 David A Black 的The Well Grounded Rubyist",这是了解 Ruby 的最佳资源之一.

And to give credit where its due - This explanation comes from "The Well Grounded Rubyist" by David A Black, one of the best resources to learn about Ruby.

这篇关于如何引用全局变量和类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆