ruby 变量类中范围_Ruby中的私有类

ruby 变量类中范围

Ruby| 私人班 (Ruby | Private Classes)

In this tutorial, we will see the concept of private classes in Ruby. Though in Ruby, the concept of private classes is a little bit different in comparison with other languages. We will understand the concept of private classes with the help of syntaxes and examples in the rest of the content.a

在本教程中,我们将看到Ruby中的私有类概念 。 尽管在Ruby中, 私有类的概念与其他语言相比还是有些不同。 我们将在其余内容中借助语法和示例来理解私有类的概念。

In Ruby language, there exists keyword like private, protected and public but they do not provide any help while working with classes that we used to get in other languages.

在Ruby语言中,存在诸如privateprotectedpublic之类的关键字,但是在使用我们以前用其他语言获得的类时,它们没有提供任何帮助。

They are differentiated by the way of calling them in the program code because all the classes are considered as objects in Ruby. Private classes can be understood as sub-class and they are declared with the help of private constants. You will have to use outer classes if you want to access the private classes. If a class is declared private then it can never be invoked with an explicit receiver, you will always have to invoke them with an implicit receiver. Ruby does not permit you to make an outer class private.

通过在程序代码中调用它们的方式来区分它们,因为在Ruby中所有类都被视为对象。 私有类可以理解为子类,它们是在私有常量的帮助下声明的。 如果要访问私有类,则必须使用外部类。 如果某个类被声明为私有,则永远不能使用显式接收器来调用它,您将始终必须使用隐式接收器来调用它们。 Ruby不允许您将外部类设为私有。

Syntax:

句法:

    private_constant: class_name

Example 1:

范例1:

=begin
  Ruby program to demonstrate private classes
=end	

class Outer
  class Inner
    def prnt1
      puts "Hello from Includehelp.com"
    end
  
    def prnt2
      puts "Corona Go! Go Corona"
    end
  end
  private_constant :Inner
end

Object1 = Outer::Inner.new
Object1.prnt2

Output

输出量

private constant Outer::Inner referenced
(repl):18:in `<main>'

Explanation:

说明:

In the above code, you can observe that the interpreter is throwing an error because we are explicit users and cannot access them in this manner.

在上面的代码中,您可以观察到解释器抛出错误,因为我们是显式用户并且无法以这种方式访问​​它们。

How you can access a private class with the help of outer-class?

如何在外部类的帮助下访问私人类?

Example 2:

范例2:

=begin
  Ruby program to demonstrate private classes
=end	

class Outer
  class Inner
    def prnt1
      puts "Hello from Includehelp.com"
    end
  
    def prnt2
      puts "Corona Go! Go Corona"
    end
  end
    private_constant :Inner
    def ob
      ob1 = Inner.new
      ob1.prnt1
      ob1.prnt2
    end
end

Object1 = Outer.new
Object1.ob

Output

输出量

Hello from Includehelp.com
Corona Go! Go Corona

Explanation:

说明:

In the above code, you can observe how you can access a private class or sub-class with the help of the public class? We have created a public method ob in which we are creating an instance of the private class Inner and invoking its methods with the help of that instance or object. This is the only way through which you can access the inner class or private class.

在上面的代码中,您可以观察到如何在公共类的帮助下访问私有类或子类? 我们创建了一个公共方法ob,在其中创建私有类Inner的实例,并在该实例或对象的帮助下调用其方法。 这是访问内部类或私有类的唯一方法。

翻译自: https://www.includehelp.com/ruby/private-classes.aspx

ruby 变量类中范围

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值