ruby访问器_Ruby中的访问控制

ruby访问器

Ruby| 访问控制 (Ruby | Access Control)

Here, we will study Access control in Ruby which is an important topic for any Object-Oriented Language and we are well aware of the fact that Ruby is a pure object-oriented language. You must have got confused with the topic but we will remove all your confusion in the rest of the content with the help of illustrating program codes and syntaxes.

在这里,我们将研究Ruby中的访问控制,这对于任何面向对象的语言都是重要的主题,并且我们很清楚Ruby是一种纯粹的面向对象的语言。 您一定对这个主题感到困惑,但是在说明程序代码和语法的帮助下,我们将消除其余所有内容中的困惑。

Access control is a way to hinder the visibility of methods and member variables to secure data from unwanted modifications but if we talk about Ruby, Ruby is slightly different from other pure object-oriented languages in terms of access control. It is different in the way that you can't apply any kind of access control to the instance and class variables as their visibility are always private. You can apply access controls to the methods.

访问控制是一种阻止方法和成员变量可见性的方法,以保护数据免受不必要的修改,但是如果我们谈论Ruby,则在访问控制方面,Ruby与其他纯面向对象的语言略有不同。 不能将任何类型的访问控制应用于实例和类变量的方式有所不同,因为它们的可见性始终是私有的。 您可以将访问控制应用于方法。

There are three types of access control available in Ruby. They are mainly public, private and protected. In C++, you use access controls only while doing Inheritance but this is not the case of Ruby.

Ruby中提供三种访问控制 。 它们主要是公共的私人的受保护的 。 在C ++中,仅在进行继承时才使用访问控制,但Ruby并非如此。

Let us discuss each access control in a detailed manner.

让我们详细讨论每个访问控制。

1)私人方法 (1) Private method)

They are the methods that can’t be accessed outside the class in which they have been declared. These methods can only access the private variables of the class. You cannot use self-keyword with private methods. Private methods can be inherited with the help of the subclass. That subclass is eligible to access them as well as can override them too.

它们是在声明它们的类之外无法访问的方法。 这些方法只能访问该类的私有变量。 您不能将自关键字与私有方法一起使用。 私有方法可以在子类的帮助下继承。 该子类可以访问它们,也可以覆盖它们。

You can create private methods in the following manner,

您可以按以下方式创建私有方法,

    private
        def method
    end

Program:

程序:

=begin
  Ruby program to demonstrate private methods
=end	

class Example
  def call
    method1
  end
  
  private
  def method1
    puts "Corona Go! Go Corona"
  end
end

obj = Example.new
obj.call

Output

输出量

Corona Go! Go Corona

Explanation:

说明:

In the above code, you can observe that we cannot access private methods directly with the help of instance of the defined class. We have invoked the method inside a public class and then have invoked the public class.

在上面的代码中,您可以看到我们无法在定义的类的实例的帮助下直接访问私有方法。 我们已经在公共类中调用了该方法,然后又调用了公共类。

2)公共方法 (2) Public methods)

All methods present in the Ruby library are public by default. Even if you declare a method, that method is also public. Public methods are accessible by everyone. They are the methods that can be invoked outside the class.

默认情况下,Ruby库中存在的所有方法都是公共的。 即使您声明一个方法,该方法也是公共的。 每个人都可以使用公共方法。 它们是可以在类外部调用的方法。

You can define a public method with the help of public keyword in the following manner,

您可以按照以下方式在public关键字的帮助下定义public方法:

    public
	    def method
    end

Program:

程序:

=begin
  Ruby program to demonstrate public methods
=end	

class Example
  public
  def method1
    puts "Corona Go! Go Corona"
  end
end

obj = Example.new
obj.method1

Output

输出量

Corona Go! Go Corona

Explanation:

说明:

You can observe that public methods can be accessed directly without the help of any other method.

您会发现,无需任何其他方法的帮助,就可以直接访问公共方法。

3)受保护的方法 (3) Protected methods)

You can only invoke protected method with the help of instances which belongs to its defined class or its subclass. Protected methods are not accessible outside the defined class or subclass. There are finite uses of such methods.

您只能在属于其定义的类或其子类的实例的帮助下调用受保护的方法。 在定义的类或子类之外无法访问受保护的方法。 这种方法有一定的用途。

You can define a protected method in the following way with the help of protected keyword,

您可以通过protected关键字,通过以下方式定义protected方法,

    protected 
	    def method
    end

Program:

程序:

=begin
  Ruby program to demonstrate protected methods
=end	

class Example
  def call
    method1
  end
  
  protected
  def method1
    puts "Corona Go! Go Corona"
  end
end

obj = Example.new
obj.call

Output

输出量

Corona Go! Go Corona

Explanation:

说明:

In the above code, you can observe that we cannot access protected methods directly with the help of instance of the defined class. We have invoked the method inside a public class and then have invoked the public class.

在上面的代码中,您可以观察到我们无法在定义的类的实例的帮助下直接访问受保护的方法。 我们已经在公共类中调用了该方法,然后又调用了公共类。

翻译自: https://www.includehelp.com/ruby/access-control.aspx

ruby访问器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值