a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > Ruby > 正文

進(jìn)一步深入Ruby中的類與對象概念

2020-10-29 19:39:39
字體:
供稿:網(wǎng)友

Ruby是純面向?qū)ο蟮恼Z言,所有項(xiàng)目似乎要Ruby中為一個(gè)對象。Ruby中的每個(gè)值是一個(gè)對象,即使是最原始的東西:字符串,數(shù)字甚至true和false。即使是一個(gè)類本身是一個(gè)對象,它是Class類的一個(gè)實(shí)例。本章將通過所有功能涉及到Ruby的面向?qū)ο蟆?/p>

類是用來指定對象的形式,它結(jié)合了數(shù)據(jù)表示和方法操縱這些數(shù)據(jù),轉(zhuǎn)換成一個(gè)整齊的包。在一個(gè)類的數(shù)據(jù)和方法,被稱為類的成員。
Ruby類的定義:

定義一個(gè)類,定義的數(shù)據(jù)類型的草圖。 這實(shí)際上并不定義任何數(shù)據(jù),但它定義的類名字的意思什么,即是什么類的對象將包括這樣一個(gè)對象上執(zhí)行什么操作可以。

類定義開始與關(guān)鍵字class類名和 end 分隔。例如,我們定義Box類使用class關(guān)鍵字如下:

class Box
   code
end

名稱必須以大寫字母開始,按照約定名稱中包含多個(gè)單詞,每個(gè)單詞沒有分隔符(駝峰式)一起執(zhí)行。
定義Ruby的對象:

類為對象的藍(lán)圖,所以基本上是一個(gè)從一個(gè)類對象被創(chuàng)建。我們聲明一個(gè)類的對象使用new關(guān)鍵字。下面的語句聲明了兩個(gè)對象,Box 類:

box1 = Box.newbox2 = Box.new

initialize方法:

initialize方法是一個(gè)標(biāo)準(zhǔn)的Ruby類的方法,和其它面向?qū)ο缶幊陶Z言的構(gòu)造方法有相同的方式工作。 initialize方法是有用的,在創(chuàng)建對象的時(shí)候,一些類變量初始化。這種方法可能需要的參數(shù)列表,它像其他Ruby之前的方法用def關(guān)鍵字定義,如下所示:

class Box
   def initialize(w,h)
      @width, @height = w, h
   end
end

實(shí)例變量:

實(shí)例變量是類的一種屬性,一旦我們使用的類對象被創(chuàng)建的對象的屬性。每個(gè)對象的屬性被分別賦值的并與其它對象共享,它們在類的內(nèi)部使用@操作符訪問,但訪問類之外的,我們使用的公共方法被稱為訪問器方法。如果我們把上述定義的類 Box,然后 @width 和 @height 類 Box實(shí)例變量。

class Box  def initialize(w,h)   # assign instance avriables   @width, @height = w, h  endend

訪問器和setter方法:

為了外部能訪問類的變量,它們必須定義存取器方法,這些存取器方法也被稱為getter方法。下面的例子演示了如何使用訪問器方法:

#!/usr/bin/ruby -w# define a classclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # accessor methods  def printWidth   @width  end  def printHeight   @height  endend# create an objectbox = Box.new(10, 20)# use accessor methodsx = box.printWidth()y = box.printHeight()puts "Width of the box is : #{x}"puts "Height of the box is : #{y}"

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Width of the box is : 10Height of the box is : 20

類似的存取方法用于訪問的變量值,Ruby提供了一種方法來從類的外部設(shè)置這些變量的值,那就是setter方法??,定義如下:

#!/usr/bin/ruby -w# define a classclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # accessor methods  def getWidth   @width  end  def getHeight   @height  end  # setter methods  def setWidth=(value)   @width = value  end  def setHeight=(value)   @height = value  endend# create an objectbox = Box.new(10, 20)# use setter methodsbox.setWidth = 30box.setHeight = 50# use accessor methodsx = box.getWidth()y = box.getHeight()puts "Width of the box is : #{x}"puts "Height of the box is : #{y}"

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Width of the box is : 30Height of the box is : 50

實(shí)例方法:

也以同樣的方式,因?yàn)槲覀兪褂胐ef關(guān)鍵字定義其他方法,并按下圖所示僅對使用一個(gè)類的實(shí)例,它們可以被用來定義該實(shí)例方法。它們的功能不局限于訪問實(shí)例變量,他們也可以按要求做更多的事情。

#!/usr/bin/ruby -w# define a classclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # instance method  def getArea   @width * @height  endend# create an objectbox = Box.new(10, 20)# call instance methodsa = box.getArea()puts "Area of the box is : #{a}"

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Area of the box is : 200

類的方法和變量:

類變量是一個(gè)變量,這是一個(gè)類的所有實(shí)例之間共享。該變量是一個(gè)實(shí)例,它是可訪問對象實(shí)例。兩個(gè)@字符類變量帶有前綴(@@)。在類定義類變量必須初始化,如下所示。

類方法的定義使用:def self.methodname() 以 end 字符結(jié)束,將被稱為使用classname.methodname類名,在下面的例子所示:

#!/usr/bin/ruby -wclass Box  # Initialize our class variables  @@count = 0  def initialize(w,h)   # assign instance avriables   @width, @height = w, h   @@count += 1  end  def self.printCount()   puts "Box count is : #@@count"  endend# create two objectbox1 = Box.new(10, 20)box2 = Box.new(30, 100)# call class method to print box countBox.printCount()

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Box count is : 2

 to_s 方法:

所定義的任何類的實(shí)例應(yīng)該有一個(gè) to_s 方法返回一個(gè)字符串形式表示對象。下面以一個(gè)簡單的例子來表示一個(gè)Box對象,在寬度和高度方面:

#!/usr/bin/ruby -wclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # define to_s method  def to_s   "(w:#@width,h:#@height)" # string formatting of the object.  endend# create an objectbox = Box.new(10, 20)# to_s method will be called in reference of string automatically.puts "String representation of box is : #{box}"

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

String representation of box is : (w:10,h:20)

訪問控制:

Ruby提供了三個(gè)級別的保護(hù)實(shí)例方法的級別:public, private 和 protected。 Ruby沒有應(yīng)用實(shí)例和類變量的任何訪問控制權(quán)。

  1.     Public Methods: 任何人都可以被稱為public方法。方法默認(rèn)為公用初始化,這始終是 private 除外。 .
  2.     Private Methods: private方法不能被訪問,或者甚至從類的外部瀏覽。只有類方法可以訪問私有成員。
  3.     Protected Methods: 受保護(hù)的方法可以被調(diào)用,只能通過定義類及其子類的對象。訪問保存在類內(nèi)部。

以下是一個(gè)簡單的例子來說明三個(gè)訪問修飾符的語法:

#!/usr/bin/ruby -w# define a classclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # instance method by default it is public  def getArea   getWidth() * getHeight  end  # define private accessor methods  def getWidth   @width  end  def getHeight   @height  end  # make them private  private :getWidth, :getHeight  # instance method to print area  def printArea   @area = getWidth() * getHeight   puts "Big box area is : #@area"  end  # make it protected  protected :printAreaend# create an objectbox = Box.new(10, 20)# call instance methodsa = box.getArea()puts "Area of the box is : #{a}"# try to call protected or methodsbox.printArea()

當(dāng)上面的代碼被執(zhí)行時(shí),產(chǎn)生下面的結(jié)果。在這里,第一種方法被調(diào)用成功,但第二種方法給一個(gè)提示。

Area of the box is : 200test.rb:42: protected method `printArea' called for #<Box:0xb7f11280 @height=20, @width=10> (NoMethodError)

類的繼承:

在面向?qū)ο蟮木幊讨凶钪匾母拍钪皇抢^承。繼承允許我們定義一個(gè)類在另一個(gè)類的項(xiàng)目,這使得它更容易創(chuàng)建和維護(hù)應(yīng)用程序。

繼承也提供了一個(gè)機(jī)會,重用代碼的功能和快速的實(shí)現(xiàn)時(shí)間,但不幸的是Ruby不支持多級的繼承,但Ruby支持混入。一個(gè)mixin繼承多重繼承,只有接口部分像一個(gè)專門的實(shí)現(xiàn)。

當(dāng)創(chuàng)建一個(gè)類,而不是寫入新的數(shù)據(jù)成員和成員函數(shù),程序員可以指定新的類繼承現(xiàn)有類的成員。這種現(xiàn)有的類稱為基類或父類和新類稱為派生類或子類。

Ruby也支持繼承。繼承和下面的例子解釋了這個(gè)概念。擴(kuò)展類的語法很簡單。只需添加一個(gè)<字符的超類聲明的名稱。例如,定義Box類的子類classBigBox:

#!/usr/bin/ruby -w# define a classclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # instance method  def getArea   @width * @height  endend# define a subclassclass BigBox < Box  # add a new instance method  def printArea   @area = @width * @height   puts "Big box area is : #@area"  endend# create an objectbox = BigBox.new(10, 20)# print the areabox.printArea()

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Big box area is : 200

方法重載:

雖然可以在派生類中添加新的函數(shù),但有時(shí)想改變的行為已經(jīng)在父類中定義的方法。只需通過保持相同的方法名和重寫該方法的功能,如下圖所示,在這個(gè)例子可以這樣做:

#!/usr/bin/ruby -w# define a classclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # instance method  def getArea   @width * @height  endend# define a subclassclass BigBox < Box  # change existing getArea method as follows  def getArea   @area = @width * @height   puts "Big box area is : #@area"  endend# create an objectbox = BigBox.new(10, 20)# print the area using overriden method.box.getArea()

運(yùn)算符重載:

我們想“+”運(yùn)算符使用+,*操作由一個(gè)標(biāo)量乘以一箱的寬度和高度,這里是一個(gè)版Box類的定義及數(shù)學(xué)運(yùn)算符:

class Box def initialize(w,h) # Initialize the width and height  @width,@height = w, h end def +(other)     # Define + to do vector addition  Box.new(@width + other.width, @height + other.height) end def -@        # Define unary minus to negate width and height  Box.new(-@width, -@height) end def *(scalar)    # To perform scalar multiplication  Box.new(@width*scalar, @height*scalar) endend

凍結(jié)對象:

有時(shí)候,我們要防止被改變的對象。凍結(jié)對象的方法可以讓我們做到這一點(diǎn),有效地把一個(gè)對象到一個(gè)恒定。任何對象都可以被凍結(jié)通過調(diào)用Object.freeze。不得修改凍結(jié)對象:不能改變它的實(shí)例變量。

可以使用Object.frozen?語句檢查一個(gè)給定的對象是否已經(jīng)被凍結(jié),被凍結(jié)的情況下的對象語句方法返回true,否則返回false值。下面的示例 freeze 的概念:

#!/usr/bin/ruby -w# define a classclass Box  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # accessor methods  def getWidth   @width  end  def getHeight   @height  end  # setter methods  def setWidth=(value)   @width = value  end  def setHeight=(value)   @height = value  endend# create an objectbox = Box.new(10, 20)# let us freez this objectbox.freezeif( box.frozen? )  puts "Box object is frozen object"else  puts "Box object is normal object"end# now try using setter methodsbox.setWidth = 30box.setHeight = 50# use accessor methodsx = box.getWidth()y = box.getHeight()puts "Width of the box is : #{x}"puts "Height of the box is : #{y}"

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Box object is frozen objecttest.rb:20:in `setWidth=': can't modify frozen object (TypeError)    from test.rb:39

類常量:

可以在類里定義分配一個(gè)直接的數(shù)字或字符串值,而不使用其定義一個(gè)變量為@@ 或 @。按照規(guī)范,我們保持常量名大寫。

一個(gè)常量一旦被定義就不能改變它的值,但可以在類里像常量一樣直接訪問,但如果要訪問一個(gè)類之外的常量,那么要使用類名::常量,所示在下面的例子。

#!/usr/bin/ruby -w# define a classclass Box  BOX_COMPANY = "TATA Inc"  BOXWEIGHT = 10  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # instance method  def getArea   @width * @height  endend# create an objectbox = Box.new(10, 20)# call instance methodsa = box.getArea()puts "Area of the box is : #{a}"puts Box::BOX_COMPANYputs "Box weight is: #{Box::BOXWEIGHT}"

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Area of the box is : 200TATA IncBox weight is: 10

類常量繼承和實(shí)例方法一樣,可以覆蓋。
創(chuàng)建對象使用分配:

當(dāng)創(chuàng)建一個(gè)對象,而不調(diào)用它的構(gòu)造函數(shù)初始化,即可能有一個(gè)情況:采用 new 方法,在這種情況下可以調(diào)用分配,這將創(chuàng)造一個(gè)未初始化的對象,看下面的例子:

#!/usr/bin/ruby -w# define a classclass Box  attr_accessor :width, :height  # constructor method  def initialize(w,h)   @width, @height = w, h  end  # instance method  def getArea   @width * @height  endend# create an object using newbox1 = Box.new(10, 20)# create another object using allocatebox2 = Box.allocate# call instance method using box1a = box1.getArea()puts "Area of the box is : #{a}"# call instance method using box2a = box2.getArea()puts "Area of the box is : #{a}"

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Area of the box is : 200test.rb:14: warning: instance variable @width not initializedtest.rb:14: warning: instance variable @height not initializedtest.rb:14:in `getArea': undefined method `*'   for nil:NilClass (NoMethodError) from test.rb:29

類信息:

如果類定義的可執(zhí)行代碼,這意味著他們在執(zhí)行的上下文中一些對象:自身都必須引用的東西。讓我們來看看它是什么。

#!/usr/bin/ruby -wclass Box  # print class information  puts "Type of self = #{self.type}"  puts "Name of self = #{self.name}"end

當(dāng)上面的代碼執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

Type of self = ClassName of self = Box

這意味著,一個(gè)類的定義,作為當(dāng)前對象的類并執(zhí)行。在元類和其超類的方法將在執(zhí)行過程中使用的方法定義。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产日韩av在线 | 一区二区三区国产免费 | 中文字幕一区二区三区四区 | 午夜免费视频 | 四虎首页 | 香蕉一区二区 | 亚洲一区在线播放 | 亚洲精品888 | 国产精品一二三区 | 亚洲精品免费视频 | t66y最新地址一地址二69 | 欧美高清视频一区二区三区 | 国产日本欧美一区二区三区 | 98精品国产高清在线xxxx天堂 | 在线看黄色av | 国产在线一区二区三区 | 妞干网免费 | 蜜臀99久久精品久久久久久软件 | 久久精品国产一区二区电影 | 有码在线 | a在线观看 | 99久久久久国产精品免费 | 亚洲日本中文 | 99精品欧美一区二区三区 | 精品欧美视频 | 亚州av在线 | 久久视频精品 | 日韩成人免费 | 精品国产一区二区在线 | 色婷婷在线播放 | 狠狠操天天干 | 国产自产才c区 | 一区二区日韩精品 | 亚洲国产精品成人综合色在线婷婷 | 日本午夜在线 | 国产极品一区二区 | 日韩无 | 国产精品美女久久久 | 欧美日性 | 自拍偷拍亚洲欧洲 | 国产99久久精品 |