简体   繁体   English

用于重命名对象的实例方法和 setter 方法有什么区别?

[英]What is the difference between an instance method used to rename an object and a setter method?

If I want to rename my jedi object below, why would I create an instance method named rename that uses the setter method name= ?如果我想在下面重命名我的jedi对象,为什么要创建一个名为rename的实例方法,它使用 setter 方法name= Why not just use the setter method `name=' directly?为什么不直接使用setter方法`name='?

Why do this:为什么要这样做:

     class Skywalker
       attr_accessor :name

       def initialize(name)
         @name = name
       end

       def rename(new_name)
         self.name = new_name
       end
     end

     jedi = Skywalker.new('Anakin')
     puts jedi.name
     jedi.rename('Luke')
     puts jedi.name

When you could just do this:当你可以这样做时:

     class Skywalker
       attr_accessor :name

       def initialize(name)
         @name = name
       end
     end

     jedi = Skywalker.new('Anakin')
     puts jedi.name
     jedi.name = 'Luke'
     puts jedi.name

Both code snippets above do the same thing, so I'm wondering if there is a situation where it would be useful to have the instance method rename in addition to the setter method name= .上面的两个代码片段都做同样的事情,所以我想知道是否存在除了 setter 方法name=之外rename实例方法有用的情况。 Because to me it looks like they are redundant.因为在我看来它们是多余的。

#rename hides the implementation details. #rename隐藏实现细节。 You expose a clean and explicit interface - an object can be renamed, but the caller doesn't have to care how it's done.您公开了一个干净而显式的接口——一个对象可以被重命名,但调用者不必关心它是如何完成的。 I would recommend to use attr_reader :name instead of attr_accessor :name to avoid exposing the setter.我建议使用attr_reader :name而不是attr_accessor :name以避免暴露 setter。

If you expose just #name= you let the caller to change object internals.如果你只公开#name=你让调用者改变对象内部。 It may cause the future changes harder (eg if you move name to a separate object).它可能会导致未来的更改更加困难(例如,如果您将name移动到一个单独的对象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 setter方法和虚方法(在Ruby中)之间有什么区别? - What's the difference between a setter method and a virtual method (in Ruby)? 方法和 proc 对象有什么区别? - What is the difference between a method and a proc object? Ruby中的object.method(a,b)和method(a,b)有什么区别 - What is the difference between object.method(a,b) and method(a,b) in Ruby 类方法,实例方法,实例变量,类变量之间的区别? - difference between class method , instance method , instance variable , class variable? :method 和:“method$##11”符号有什么区别? - What is the difference between :method and :“method$##11” symbol? instance_eval和singleton方法之间的区别 - Difference between instance_eval and singleton method 在类和方法中定义类实例变量之间有什么区别? 为什么? - What's the difference between defining class instance variable in a class and a method. Why? instance&class 方法 include&extend 有什么区别(Ruby、Rails) - What is the difference between instance&class method include&extend (Ruby, Rails) 别名方法和别名符号有什么区别? - What is the difference between alias method and alias symbol? named_scope和method有什么区别? - what is the difference between named_scope and method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM