红宝石中的私有方法与受保护方法 [英] Private vs Protected method in ruby

查看:108
本文介绍了红宝石中的私有方法与受保护方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个方法受保护,则定义类或其子类的任何实例都可以将其称为
。如果某个方法是私有的,则可能仅在调用对象的上下文中调用
,即使该对象与该对象属于同一类,也永远不可能直接访问
另一个对象的私有方法。
调用者。

If a method is protected, it may be called by any instance of the defining class or its subclasses. If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller.

当我在Ruby中搜索私有方法和受保护方法之间的区别时,我从网上得到了这个定义。

This definition i got from net when searching for the difference between private and protected methods in ruby.

我对此有2个疑问

class Abc
  def abc
    xyz
  end
 protected 
  def xyz
    p "hai"
  end
end

a=Abc.new
a.abc

在这种情况下,我隐式地在对象a上调用xyz,这就是我将如何称呼xyz,即使它因此,定义类的任何实例都可以调用它是什么意思?????

In this i am calling xyz implicitly on object a, this is how i will call xyz even if it is private.So "it may be called by any instance of the defining class" what does this mean ?????

其次,

 class Abc
   def abc(obj)
     obj.xyz1
     obj.xyz2
     obj.xyz3
   end
 end

 class Xyz
   def xyz1
     p "called"
   end

  private 
   def xyz2
     p "called"
   end

  protected
   def xyz3
     p "called"
   end

 end

 a=Abc.new
 b=Xyz.new
 a.abc(b)

在这种情况下,我可以在对象a上调用obj b的xyz1方法。无法对对象a调用obj b的受保护的xyz3和私有方法xyz2方法。因此,即使对象与调用者属于同一类,也永远无法直接访问另一个对象的私有方法。这确切是什么意思?

In this i can call obj b's xyz1 method on object a.But i cant call obj b's protected xyz3 and private method xyz2 method on object a.So "it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller." what does this exact mean???

有人可以帮助我以更好的例子来理解这一点吗??

can anybody help me to understand this one with better examples????

推荐答案

私有方法



要定义私有方法,我们使用private关键字,它实际上是一个内置方法在名为Module的类中实现。私有方法只能由定义了它的类(或其子类之一)内的另一种方法调用。

Private Methods

To define a private method, we use the private keyword, which is actually a built-in method implemented in a class called Module. A private method can only be called by another method within the class on which it was defined (or one of its subclasses).

class Koan
  def call_say_koan
    say_koan
  end

  private
    def say_koan
      puts "What is the sound of one hand clapping?"
    end
end

k = Koan.new
k.say_koan    # Output: NoMethodError: private method `say_koan' called for #<Koan:0x000000021e7380>
k.call_say_koan        # Output: What is the sound of one hand clapping?

在上面的示例中,我们无法直接(从类外部)调用say_koan私有方法,但是我们可以调用call_say_koan公共方法,该公共方法又称为say_koan。

In the above example, we could not call the say_koan private method directly (from outside the class), but we could call the call_say_koan public method which, in turn, called say_koan.

在上面的示例中,内置的私有方法也没有参数使用。因此,下面定义的所有方法都被设为私有。

Also in the above example, the built-in private method was used with no arguments. Hence, all methods defined below it were made private.

私有方法也可以与先前定义的方法名称(作为符号传递)一起用作参数。

The private method can also be used with previously defined method names (passed as symbols) as arguments.

class Foo
  def some_method
  end

  private :some_method
end

为了将类方法设为私有,请使用private_class_method关键字/方法而不是private。

In order to make a class method private, use the private_class_method keyword/method instead of private.

私有方法不能用接收者调用,例如self。尝试使用self作为call_say_koan中的接收者(self.say_koan)来调用say_koan方法将导致以下异常:

Private methods can't be called with a receiver, such as self. Trying to call the say_koan method with self as a receiver (self.say_koan) within call_say_koan would result in the following exception:

NoMethodError: private method `say_koan' called for #<Koan:0x000000021eb548>

从Ruby 2.0开始,response_to吗?给定私有方法作为参数时,该方法将返回false。

As of Ruby 2.0, the respond_to? method will return false when given a private method as an argument.

k.respond_to? :say_koan  # Output: => false

要列出类中的所有私有实例方法,请使用private_instance_methods内置方法。对于私有类方法,请使用private_methods。

To list all private instance methods in a class, use the private_instance_methods built-in method. For private class methods, use private_methods.

Koan.private_instance_methods(false)  # Output => [:say_koan]



受保护的方法



要定义一个受保护的方法,我们使用protected关键字(实际上是一个方法)。与私有方法一样,受保护的方法也可以由定义该方法的类(或其子类之一)中的其他方法调用。区别是,也可以从同一类的其他实例中调用受保护的方法。

Protected Methods

To define a protected method, we use the protected keyword (which is actually a method). Like private methods, protected methods can also be called by other methods within the class on which it was defined (or one of its subclasses). The difference is, protected methods can also be called from within other instances of the same class.

没有受保护的类方法之类的东西,Ruby仅支持受保护的方法

There is no such thing as a protected a class method, Ruby only supports protected instance methods.

让我们假设我们需要选择一些冥想者来参加研究。为了找到经验最丰富的禅修者,我们需要比较他们的冥想总时间。但是,我们不希望看到小时数。

Let's suppose we need to select a few meditators to participate in a study. To find the most experienced meditators, we need to compare their total hours of meditation. However, we don't want the number of hours to be visible.

class Meditator
  def initialize(hours)
    @hours = hours
  end

  def more_experienced?(other_person)
    hours > other_person.hours
  end

  protected
    attr_reader :hours  # We have made the accessor protected
end

m1 = Meditator.new 3000
m2 = Meditator.new 5000

m2.more_experienced? m1  # Output: => true
m1.more_experienced? m2  # Output: => false

类似的代码可用于保护任何敏感数据,使其免受外部访问(在类外部和

Similar code could be used to protect any kind of sensitive data from outside access (outside the class and its instances), although protected methods are not commonly employed in Ruby.

当不带参数调用时(如上例所示),protected方法将转换下面定义的所有方法将其转换为受保护的方法。它也可以用来保护先前定义的方法,如下例所示。

When called with no arguments (as in the above example), the protected method turns all methods defined below it into protected methods. It can also be used to protect previously defined methods, as in the following example.

class Foo
  def some_method
  end

  protected :some_method
end

到列出一个类中的所有受保护实例方法,请使用protected_instance_methods内置方法。对于受保护的类方法,请使用protected_methods。

To list all protected instance methods in a class, use the protected_instance_methods built-in method. For protected class methods, use protected_methods.

Meditator.protected_instance_methods(false)  # Output: => [:hours]

这篇关于红宝石中的私有方法与受保护方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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