ruby 变量类中范围
Ruby类 (Ruby Classes)
In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object or instance of the class 'laptop'. Thus, we can conclude that "A class is a blueprint or prototype that includes some methods and data members which are common to objects which are identical to each other in some way".
在现实世界中,我们有许多属于同一类别的对象。 例如,我在笔记本电脑上工作,而该笔记本电脑就是全球范围内存在的那些笔记本电脑之一。 因此,此便携式计算机是“笔记本电脑”类的对象或实例。 因此,我们可以得出结论: “一个类是一个蓝图或原型,它包含一些在某种程度上彼此相同的对象所共有的方法和数据成员” 。
Ruby is purely Object Oriented which means that its code may contain several classes and their instances. In Ruby, the concept of Reusability is achieved through the class as it allows the programmer to use the same prototype again and again to create objects which are similar to each other.
Ruby纯粹是面向对象的,这意味着它的代码可能包含几个类及其实例。 在Ruby中,可重用性的概念是通过类实现的,因为它允许程序员反复使用相同的原型来创建彼此相似的对象。
Class hierarchy in Ruby is demonstrated through the following diagram:
下图演示了Ruby中的类层次结构 :
![Classes in Ruby programming language](https://i-blog.csdnimg.cn/blog_migrate/c2dceefbd2473efef5a3ee2cebef700f.png)
Ruby provides you many classes. The above diagram does not include all of them. Class BasicObject is the superclass of all the classes in Ruby.
Ruby为您提供了许多类。 上图未涵盖所有内容。 BasicObject 类是Ruby中所有类的超 类 。
在Ruby中创建类 (Creating Class in Ruby)
Creating class in Ruby is not a very difficult task in Ruby. Its definition starts with the keyword "class" and closes with the keyword 'end'. Its basic syntax is as follows:
在Ruby中创建类并不是在Ruby中的艰巨任务。 其定义以关键字“ class”开头,以关键字'end'结束 。 其基本语法如下:
class (class _name)
end
Example:
例:
class Student
end
The class "Student" is having no data member as well as a member function. We can create a class with data member and methods in the following manner:
类“学生”没有数据成员以及成员函数。 我们可以通过以下方式用数据成员和方法创建一个类:
class (class_name)
def (method_name)
end
end
We can create its object with the help of new keyword as shown below:
我们可以借助new关键字创建其对象,如下所示:
(instance_name) = (classname).new(parameters)
We can call its methods by using . operator like,
我们可以使用调用它的方法。 操作员喜欢
(instance_name).(method_name)
Example:
例:
class Student
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
输出量
Enter the number of students
36
The updated numbers of students are 36
In the above example, we have created a class Student. A method update is defined inside it with a local variable no_of_students.
在上面的示例中,我们创建了一个Student类。 在其中使用局部变量no_of_students定义方法更新 。
In the main(), we have created an object or instance of the class Student and named it as record1. By using . operator with the instance name, we are accessing the method update.
在main()中 ,我们创建了Student类的对象或实例,并将其命名为record1 。 通过使用。 实例名称的操作符,我们正在访问方法update 。
类可见性 (Class Visibility)
Private
私人的
Public
上市
Protected
受保护的
1) Private
1)私人的
Private methods can only be invoked in the context of the current object. You cannot call the private method directly in the main() method, If you are doing so, you will get an error as the visibility of private method is limited to the class in which it has been created.
私有方法只能在当前对象的上下文中调用。 您不能直接在main()方法中调用私有方法。如果这样做,您将得到一个错误,因为私有方法的可见性仅限于创建它的类。
For making a method Private, you need to use the keyword "private" before defining the method.
为了使方法私有,您需要在定义方法之前使用关键字“私有” 。
Syntax:
句法:
private
def (method_name)
end
Example:
例:
class Student
private
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
输出量
// cannot use private members...
`<main>': private method `update' called for
#<Student:0x000000018998a0> (NoMethodError)
2) Public
2)公开
by default, every method is public in Ruby i.e. they are free to be used by anyone – no access control is applied to them. In any case, if we explicitly want to make a method public, then we have to use the keyword 'public' along with the name of the method.
默认情况下,每种方法在Ruby中都是公共的,也就是说,任何人都可以自由使用它们-不对其应用访问控制。 在任何情况下,如果我们明确希望将方法公开,则必须使用关键字“ public”以及方法名称。
Syntax:
句法:
public
def (method_name)
end
Example:
例:
class Student
public
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
输出量
Enter the number of students
36
The updated numbers of students are 36
3) Protected
3)受保护
Protected methods are only accessible to the objects of defining class and its child class or subclass. They are mainly used during Inheritance (Parent-Child class Concept). The keyword "rotected" is used before the method name.
受保护的方法仅可用于定义类及其子类或子类的对象。 它们主要在继承期间使用(父子类Concept)。 在方法名称之前使用关键字“ rotected” 。
Syntax:
句法:
protected
def (method_name)
end
Example:
例:
class Student
protected
def update
puts "Enter the number of students"
no_of_students=gets.chomp
puts "The updated numbers of students are #{no_of_students}"
end
end
record1=Student.new
record1.update
Output
输出量
`<main>': protected method `update' called for
#<Student:0x00000001e90948> (NoMethodError)
ruby 变量类中范围