尝试学习/理解 Ruby 的 setter 和 getter 方法 [英] Trying to learn / understand Ruby setter and getter methods

查看:47
本文介绍了尝试学习/理解 Ruby 的 setter 和 getter 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚学习编程,并决定尝试 Ruby.我确定这是一个愚蠢的问题,但讲师正在谈论 setter 和 getter 方法,我很困惑.例子如下:

I'm just learning to program and have decided to try Ruby. I'm sure this is a stupid question, but the instructor is talking about setter and getter methods, and I'm confused. Here is the example:

class Human
  def noise=(noise)
    @noise = noise
  end

  def noise
    @noise
  end
end

由此,类被实例化,我可以把它放出来:

From this, the class is instantiated, and I can puts this out:

man = Human.new
man.noise=("Howdie!")
puts man.noise

结果是 Howdie!

现在让我感到困惑的是,讲师说如果没有 getter 方法(两种方法中的第二种),就无法与实例变量 @noise 交互.

Now what confuses me is that the instructor is saying without the getter method (the 2nd of the two methods), there is no way to interact with the instance variable @noise.

但是当我删除 getter 方法时,我仍然可以访问 @noise,请参见示例:

But when I remove the getter method, I'm able to still access @noise, see example:

class Human
  def noise=(noise)
    @noise = noise
  end
end

man = Human.new
puts man.noise=("Howdie!")

这与它使用的 getter 方法相同.

This works the same as when the getter method it used.

所以现在我很困惑.为什么需要吸气剂?没有它就无法访问实例变量是什么意思?他可能使用的是旧版本的 Ruby 吗?

So now I'm confused. Why is the getter needed? What does the instructor mean by not being able to access the instance variable without it? Is it possible he's using an older version of Ruby?

预先感谢您的帮助.

推荐答案

即使没有 getter,您也可以从属于该实例的其他方法与该实例变量交互:

You can interact with that instance variable from other methods belonging to that instance, even if there is no getter:

def noise=(noise)
  @noise = noise
end

def last_noise
  @noise
end

不需要定义与方法同名的getter;两者根本没有联系.需要使用 getter 来获取"实例变量的值,但只能使用 short 语法.

There doesn't need to be a getter defined with the same name as the method; the two are not linked at all. The getter is needed to "get" the value of the instance variable, but only in a short syntax.

您的示例中发生的事情是您正在初始化一个新对象 (Human.new),然后使用一个方法(noise=,是的方法名称包含 = 符号),它恰好定义了一个实例变量(即,一个变量 just 用于该实例),然后最后检索该实例变量另一个方法调用.

What's happening in your example is that you're initializing a new object (Human.new), and then using a method (noise=, yes the method name contains the = symbol) that just-so-happens to define an instance variable (that is, a variable just for that instance), and then finally retrieving that instance variable with another method call.

您实际上可以使用 instance_variable_get 来获取实例变量,而无需定义 any getter:

You can actually use instance_variable_get to get the instance variable without defining any getter at all:

man = Human.new
man.noise = "Howdie"
man.instance_variable_get("@noise")

这将返回Howdie",即使没有定义 getter.

This will return "Howdie", even though there is no getter defined.

不,我不认为他在使用旧版本的 Ruby.

And no, I don't think he's using an older version of Ruby.

这篇关于尝试学习/理解 Ruby 的 setter 和 getter 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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