日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ruby 新建对象_Ruby中的面向对象编程

發布時間:2025/3/11 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby 新建对象_Ruby中的面向对象编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ruby 新建對象

Before getting into understanding how Object-oriented programming is implemented in Ruby, let us first understand what Object Oriented means.

在了解如何在Ruby中實現面向對象的編程之前,讓我們首先了解面向對象的含義。

Object-oriented programming reduces the complexity of large software systems thus making the software easier to maintain. This category of programming basically uses Objects. In a pure object-oriented language, everything is considered as an Object. The main aim of OOP is to combine the data and functions which are operating those data so that the data cannot be accessed by any other part of the code.

面向對象的編程降低了大型軟件系統的復雜性,從而使軟件易于維護。 這類編程基本上使用對象。 在純面向對象的語言中,所有內容都被視為對象。 OOP的主要目的是將數據和操作這些數據的功能組合在一起,以使代碼的任何其他部分都無法訪問該數據。

Now let us talk about Ruby. Ruby is a pure Object oriented language and everything is considered as an object. Even Strings, Numbers, true or false is an Object being the most primitive type. Rest of the article will let you know, how classes and objects are implemented in Ruby?

現在讓我們談談Ruby。 Ruby是一種純面向對象的語言 ,所有內容都被視為對象。 甚至字符串,數字,true或false也是最原始的對象類型。 本文的其余部分將讓您知道,如何在Ruby中實現類和對象?

(Class)

Class is nothing but a blueprint of a data type. It simply defines, what an instance of the class consists and possible functions which can be performed on the object of the class. In Ruby, even classes are objects of "class" class.

類不過是數據類型的藍圖。 它僅定義類的實例由什么組成,以及可以對類的對象執行的可能功能。 在Ruby中,甚至類都是“類”類的對象。

Syntax:

句法:

class Class_name#codeend

Remember that, the class name must start with a capital letter by convention. You will get an error at Compile time when the convention is not followed.

請記住,按照慣例,類名必須以大寫字母開頭。 如果不遵守約定,則會在編譯時出現錯誤。

Now, let us declare a class in Ruby,

現在,讓我們在Ruby中聲明一個類,

class Exampledef initializeenddef printsendend

The above code will be compiled but not yield an output because no memory is provided to the class until it is not instantiated.

上面的代碼將被編譯,但不會產生輸出,因為在未實例化該類之前,不會為該類提供任何內存。

對象 (Objects)

Objects are the instance of a class. They provide memory to the class. You can create any number of objects of a class. The objects are created with the help of the "new" keyword.

對象是類的實例。 它們為班級提供記憶。 您可以創建任何數量的類的對象。 在“ new”關鍵字的幫助下創建對象。

Syntax:

句法:

object_name = class_name.new

Let us understand object creation with the help of an example:

讓我們借助示例來了解對象創建:

class Exampledef initializeenddef printsputs "Hello fella. How are you!!"end endob1 = Example.new ob1.prints ob2 = Example.new ob2.prints

Output

輸出量

Hello fella. How are you!! Hello fella. How are you!!

In the above code, you can observe that we are creating two objects of class Example. Then we are invoking prints method with the instances.

在上面的代碼中,您可以觀察到我們正在創建類Example的兩個對象。 然后,我們將實例調用prints方法。

建設者 (Constructors)

Constructors are used to initialize the variable of a class. They initialize class variables at the time of object creation. ‘initialize’ method works as a constructor in Ruby. It is defined inside the class and is invoked with the creation of an object. Go through the syntax and example for a better understanding.

構造函數用于初始化類的變量。 它們在創建對象時初始化類變量。 'initialize'方法在Ruby中充當構造函數。 它在類內部定義,并在創建對象時調用。 通過語法和示例可以更好地理解。

Syntax:

句法:

class Class_namedef initialize(parameters if required)endend

Example:

例:

=begin Ruby program to demonstrate initialize. =end class Exampledef initialize(j,k)@a = k@b = jenddef printsputs "The value of class variables are #{@a} and #{@b}"end endob1 = Example.new(2,5) ob1.prints ob2 = Example.new(9,7) ob2.prints

Output

輸出量

The value of class variables are 5 and 2 The value of class variables are 7 and 9

翻譯自: https://www.includehelp.com/ruby/object-oriented-programming.aspx

ruby 新建對象

總結

以上是生活随笔為你收集整理的ruby 新建对象_Ruby中的面向对象编程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。