ruby 變量類中范圍_Ruby中的類

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

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)

  1. Private

    私人的

  2. Public

    上市

  3. 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 變量類中范圍

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540517.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540517.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540517.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

以云計算的名義 駐云科技牽手阿里云

本文講的是以云計算的名義 駐云科技牽手阿里云一次三個公司的牽手 可能會改變無數企業的命運 2017年4月17日&#xff0c;對于很多人來說可能只是個平常的工作日&#xff0c;但是對于國內無數的企業來說卻可能是個會改變企業命運的日。駐云科技聯合國內云服務提供商阿里云及國外…

[轉載] python學習筆記

參考鏈接&#xff1a; Python | a b并不總是a a b 官網http://www.python.org/ 官網library http://docs.python.org/library/ PyPI https://pypi.python.org/pypi 中文手冊&#xff0c;適合快速入門 http://download.csdn.net/detail/xiarendeniao/4236870 py…

標志寄存器_訪問標志寄存器,并與寄存器B |交換標志寄存器F的內容 8085微處理器...

標志寄存器Problem statement: 問題陳述&#xff1a; Write an assembly language program in 8085 microprocessor to access Flag register and exchange the content of flag register F with register B. 在8085微處理器中編寫匯編語言程序以訪問標志寄存器&#xff0c;并…

瀏覽器端已支持 ES6 規范(包括 export import)

當然&#xff0c;是幾個比較優秀的瀏覽器&#xff0c;既然是優秀的瀏覽器&#xff0c;大家肯定知道是那幾款啦&#xff0c;我就不列舉了&#xff0c;我用的是 chrome。 對 script 聲明 type 為 module 后就可以享受 es6 規范所帶來的模塊快感了。 基礎語法既然是全支持&#xf…

[轉載] Python學習:Python成員運算符和身份運算符

參考鏈接&#xff1a; Python中和is運算符之間的區別 Python成員運算符 除了以上的一些運算符之外&#xff0c;Python還支持成員運算符&#xff0c;測試實例中包含了一系列的成員&#xff0c;包括字符串&#xff0c;列表或元組。 運算符 描述 實例 in 如果在指定的序列中找…

量詞邏輯量詞里面的v表示?_代理知識表示中的量詞簡介(基于人工智能)

量詞邏輯量詞里面的v表示&#xff1f;As we know that in an AI-based agent, the knowledge is represented through two types of logic: The propositional logic and the predicate logic. In the propositional logic, we have declarative sentences, and in the predica…

[轉載] Python 機器學習經典實例

參考鏈接&#xff1a; Python中的邏輯門 內容介紹 在如今這個處處以數據驅動的世界中&#xff0c;機器學習正變得越來越大眾化。它已經被廣泛地應用于不同領域&#xff0c;如搜索引擎、機器人、無人駕駛汽車等。本書首先通過實用的案例介紹機器學習的基礎知識&#xff0c;然后…

哈希表的最差復雜度是n2_給定數組A []和數字X,請檢查A []中是否有對X | 使用哈希O(n)時間復雜度| 套裝1...

哈希表的最差復雜度是n2Prerequisite: 先決條件&#xff1a; Hashing data structure 散列數據結構 Problem statement: 問題陳述&#xff1a; Given an array and a sum X, fins any pair which sums to X. Expected time complexity O(n). 給定一個數組和一個和X &#xff…

一文讀懂深度學習框架下的目標檢測(附數據集)

從簡單的圖像分類到3D位置估算&#xff0c;在機器視覺領域里從來都不乏有趣的問題。其中我們最感興趣的問題之一就是目標檢測。 如同其他的機器視覺問題一樣&#xff0c;目標檢測目前為止還沒有公認最好的解決方法。在了解目標檢測之前&#xff0c;讓我們先快速地了解一下這個領…

[轉載] Python-Strings

參考鏈接&#xff1a; Python成員資格和身份運算符 &#xff5c; in, not in, is, is not Strings 介紹 String是Python中最常用的類型。僅僅用引號括起字符就可以創建string變量。字符串使用單引號或雙引號對Python來說是一樣的。 var1 Hello World! var2 "Pyth…

aes-128算法加密_加密算法問題-人工智能中的一種約束滿意問題

aes-128算法加密The Crypt-Arithmetic problem in Artificial Intelligence is a type of encryption problem in which the written message in an alphabetical form which is easily readable and understandable is converted into a numeric form which is neither easily…

讀書筆記《集體智慧編程》Chapter 2 : Make Recommendations

本章概要本章主要介紹了兩種協同過濾&#xff08;Collaborative Filtering&#xff09;算法&#xff0c;用于個性化推薦&#xff1a;基于用戶的協同過濾&#xff08;User-Based Collaborative Filtering&#xff0c;又稱 K-Nearest Neighbor Collaborative Filtering&#xff0…

[轉載] python中的for循環對象和循環退出

參考鏈接&#xff1a; Python中循環 流程控制-if條件 判斷條件&#xff0c;1位true&#xff0c;0是flesh&#xff0c;成立時true&#xff0c;不成立flesh&#xff0c;not取反 if 1; print hello python print true not取反&#xff0c;匹配取反&#xff0c;表示取非1…

設計一個應用程序,以在C#中的按鈕單擊事件上在MessageBox中顯示TextBox中的文本...

Here, we took two controls on windows form that are TextBox and Button, named txtInput and btnShow respectively. We have to write C# code to display TextBox’s text in the MessageBox on Button Click. 在這里&#xff0c;我們在Windows窗體上使用了兩個控件&…

Oracle優化器:星型轉換(Star Query Transformation )

Oracle優化器&#xff1a;星型轉換&#xff08;Star Query Transformation &#xff09;Star query是一個事實表&#xff08;fact table&#xff09;和一些維度表&#xff08;dimension&#xff09;的join。每個維度表都跟事實表通過主外鍵join&#xff0c;且每個維度表之間不j…

[轉載] python循環中break、continue 、exit() 、pass的區別

參考鏈接&#xff1a; Python中的循環和控制語句(continue, break and pass) 1、break&#xff1a;跳出循環&#xff0c;不再執行 用在while和for循環中 用來終止循環語句&#xff0c;即循環條件沒有False條件或者序列還沒被完全遞歸完&#xff0c;也會停止執行循環語句 如果…

JavaScript | 聲明數組并使用數組索引分配元素的代碼

Declare an array, assign elements by indexes and print all elements in JavaScript. 聲明一個數組&#xff0c;通過索引分配元素&#xff0c;并打印JavaScript中的所有元素。 Code: 碼&#xff1a; <html><head><script>var fruits [];fruits[0]"…

[轉載] Python入門(輸入/輸出、數據類型、條件/循環語句)

參考鏈接&#xff1a; Python中的循環技術 在介紹之前我們先來看看計算機的三個根本性基礎&#xff1a; 1.計算機是執行輸入、運算、輸出的機器 2.程序是指令和數據的集合 3.計算機的處理方式有時與人們的思維習慣不同 &#xff08;以上是引自《計算機是怎樣跑起來的》…

第5章 函數與函數式編程

第5章 函數與函數式編程 凡此變數中函彼變數者&#xff0c;則此為彼之函數。 ( 李善蘭《代數學》) 函數式編程語言最重要的基礎是λ演算&#xff08;lambda calculus&#xff09;&#xff0c;而且λ演算的函數可以傳入函數參數&#xff0c;也可以返回一個函數。函數式編程 (簡稱…

mcq 隊列_人工智能能力問答中的人工智能概率推理(MCQ)

mcq 隊列1) Which of the following correctly defines the use of probabilistic reasoning in AI systems? In situations of uncertainty, probabilistic theory can help us give an estimate of how much an event is likely to occur or happen.It helps to find the pr…