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中的類層次結構 :
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)
翻譯自: https://www.includehelp.com/ruby/classes.aspx
ruby 變量類中范圍