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#codeendRemember 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 printsendendThe 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.newLet 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.printsOutput
輸出量
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)endendExample:
例:
=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.printsOutput
輸出量
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中的面向对象编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hashset java_Java Ha
- 下一篇: 分披萨问题_比萨疯狂问题