ruby 新建对象_Ruby中的面向对象编程
ruby 新建對象
Before getting into understanding how Object-oriented programming is implemented in Ruby, let us first understand what Object Oriented means.
在了解如何在Ruby中實現(xiàn)面向?qū)ο蟮木幊讨?#xff0c;讓我們首先了解面向?qū)ο蟮暮x。
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.
面向?qū)ο蟮木幊探档土舜笮蛙浖到y(tǒng)的復(fù)雜性,從而使軟件易于維護。 這類編程基本上使用對象。 在純面向?qū)ο蟮恼Z言中,所有內(nèi)容都被視為對象。 OOP的主要目的是將數(shù)據(jù)和操作這些數(shù)據(jù)的功能組合在一起,以使代碼的任何其他部分都無法訪問該數(shù)據(jù)。
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?
現(xiàn)在讓我們談?wù)凴uby。 Ruby是一種純面向?qū)ο蟮恼Z言 ,所有內(nèi)容都被視為對象。 甚至字符串,數(shù)字,true或false也是最原始的對象類型。 本文的其余部分將讓您知道,如何在Ruby中實現(xiàn)類和對象?
類 (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.
類不過是數(shù)據(jù)類型的藍(lán)圖。 它僅定義類的實例由什么組成,以及可以對類的對象執(zhí)行的可能功能。 在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.
請記住,按照慣例,類名必須以大寫字母開頭。 如果不遵守約定,則會在編譯時出現(xiàn)錯誤。
Now, let us declare a class in Ruby,
現(xiàn)在,讓我們在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.
上面的代碼將被編譯,但不會產(chǎn)生輸出,因為在未實例化該類之前,不會為該類提供任何內(nèi)存。
對象 (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.
對象是類的實例。 它們?yōu)榘嗉壧峁┯洃洝?您可以創(chuàng)建任何數(shù)量的類的對象。 在“ new”關(guān)鍵字的幫助下創(chuàng)建對象。
Syntax:
句法:
object_name = class_name.newLet us understand object creation with the help of an example:
讓我們借助示例來了解對象創(chuàng)建:
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.
在上面的代碼中,您可以觀察到我們正在創(chuàng)建類Example的兩個對象。 然后,我們將實例調(diào)用prints方法。
建設(shè)者 (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.
構(gòu)造函數(shù)用于初始化類的變量。 它們在創(chuàng)建對象時初始化類變量。 'initialize'方法在Ruby中充當(dāng)構(gòu)造函數(shù)。 它在類內(nèi)部定義,并在創(chuàng)建對象時調(diào)用。 通過語法和示例可以更好地理解。
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 新建對象
總結(jié)
以上是生活随笔為你收集整理的ruby 新建对象_Ruby中的面向对象编程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hashset java_Java Ha
- 下一篇: 分披萨问题_比萨疯狂问题