1. 程式人生 > >雷林鵬分享:Ruby 面向物件

雷林鵬分享:Ruby 面向物件

  Ruby 面向物件

  Ruby 是純面向物件的語言,Ruby 中的一切都是以物件的形式出現。Ruby 中的每個值都是一個物件,即使是最原始的東西:字串、數字,甚至連 true 和 false 都是物件。類本身也是一個物件,是 Class 類的一個例項。本章將向您講解所有與 Ruby 面向物件相關的主要功能。

  類用於指定物件的形式,它結合了資料表示法和方法,把資料整理成一個整齊的包。類中的資料和方法被稱為類的成員。

  Ruby 類定義

  當您定義一個類時,您實際是定義了一個數據型別的藍圖。這實際上並沒有定義任何的資料,而是定義了類的名稱意味著什麼,也就是說,定義了類的物件將由什麼組成,以及在該物件上能執行什麼操作。

  類定義以關鍵字 class 開始,後跟類名稱,最後以一個 end 進行分隔表示終止該類定義。例如,我們使用關鍵字 class 來定義 Box 類,如下所示:

  class Box

  code

  end

  按照慣例,名稱必須以大寫字母開頭,如果包含多個單詞,每個單詞首字母大寫,但此間沒有分隔符(例如:CamelCase)。

  定義 Ruby 物件

  類提供了物件的藍圖,所以基本上,物件是根據類進行建立的。我們使用 new 關鍵字宣告類的物件。下面的語句聲明瞭類 Box 的兩個物件:

  box1 = Box.new

  box2 = Box.new

  initialize 方法

  initialize 方法是一個標準的 Ruby 類方法,與其他面向物件程式語言中的 constructor 工作原理類似。當您想要在建立物件的同時初始化一些類變數,initialize 方法就派上用場了。該方法帶有一系列引數,與其他 Ruby 方法一樣,使用該方法時,必須在前面放置 def 關鍵字,如下所示:

  class Box

  def initialize(w,h)

  @width, @height = w, h

  end

  end

  例項變數

  例項變數是類屬性,它們在使用類建立物件時就變成物件的屬性。每個物件的屬性是單獨賦值的,和其他物件之間不共享值。在類的內部,是使用 @ 運算子訪問這些屬性,在類的外部,則是使用稱為訪問器方法的公共方法進行訪問。下面我們以上面定義的類 Box 為例項,把 @width 和 @height 作為類 Box 的例項變數。

  class Box

  def initialize(w,h)

  # 給例項變數賦值

  @width, @height = w, h

  end

  end

  訪問器 & 設定器 方法

  為了在類的外部使用變數,我們必須在訪問器方法內部定義這些變數,這些訪問器方法也被稱為獲取器方法。下面的例項演示了訪問器方法的用法:

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 訪問器方法

  def printWidth

  @width

  end

  def printHeight

  @height

  end

  end

  # 建立物件

  box = Box.new(10, 20)

  # 使用訪問器方法

  x = box.printWidth()

  y = box.printHeight()

  puts "Width of the box is : #{x}"

  puts "Height of the box is : #{y}"

  當上面的程式碼執行時,它會產生以下結果:

  Width of the box is : 10

  Height of the box is : 20

  與用於訪問變數值的訪問器方法類似,Ruby 提供了一種在類的外部設定變數值的方式,也就是所謂的設定器方法,定義如下:

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 訪問器方法

  def getWidth

  @width

  end

  def getHeight

  @height

  end

  # 設定器方法

  def setWidth=(value)

  @width = value

  end

  def setHeight=(value)

  @height = value

  end

  end

  # 建立物件

  box = Box.new(10, 20)

  # 使用設定器方法

  box.setWidth = 30

  box.setHeight = 50

  # 使用訪問器方法

  x = box.getWidth()

  y = box.getHeight()

  puts "Width of the box is : #{x}"

  puts "Height of the box is : #{y}"

  當上面的程式碼執行時,它會產生以下結果:

  Width of the box is : 30

  Height of the box is : 50

  例項方法

  例項方法的定義與其他方法的定義一樣,都是使用 def 關鍵字,但它們只能通過類例項來使用,如下面例項所示。它們的功能不限於訪問例項變數,也能按照您的需求做更多其他的任務。

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  # constructor method

  def initialize(w,h)

  @width, @height = w, h

  end

  # 例項方法

  def getArea

  @width * @height

  end

  end

  # 建立物件

  box = Box.new(10, 20)

  # 呼叫例項方法

  a = box.getArea()

  puts "Area of the box is : #{a}"

  當上面的程式碼執行時,它會產生以下結果:

  Area of the box is : 200

  類方法 & 類變數

  類變數是在類的所有例項中共享的變數。換句話說,類變數的例項可以被所有的物件例項訪問。類變數以兩個 @ 字元(@@)作為字首,類變數必須在類定義中被初始化,如下面例項所示。

  類方法使用 def self.methodname() 定義,類方法以 end 分隔符結尾。類方法可使用帶有類名稱的 classname.methodname 形式呼叫,如下面例項所示:

  #!/usr/bin/ruby -w

  class Box

  # 初始化類變數

  @@count = 0

  def initialize(w,h)

  # 給例項變數賦值

  @width, @height = w, h

  @@count += 1

  end

  def self.printCount()

  puts "Box count is : #@@count"

  end

  end

  # 建立兩個物件

  box1 = Box.new(10, 20)

  box2 = Box.new(30, 100)

  # 呼叫類方法來輸出盒子計數

  Box.printCount()

  當上面的程式碼執行時,它會產生以下結果:

  Box count is : 2

  to_s 方法

  您定義的任何類都有一個 to_s 例項方法來返回物件的字串表示形式。下面是一個簡單的例項,根據 width 和 height 表示 Box 物件:

  #!/usr/bin/ruby -w

  class Box

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 定義 to_s 方法

  def to_s

  "(w:#@width,h:#@height)" # 物件的字串格式

  end

  end

  # 建立物件

  box = Box.new(10, 20)

  # 自動呼叫 to_s 方法

  puts "String representation of box is : #{box}"

  當上面的程式碼執行時,它會產生以下結果:

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

  訪問控制

  Ruby 為您提供了三個級別的例項方法保護,分別是 public、private 或 protected。Ruby 不在例項和類變數上應用任何訪問控制。

  Public 方法: Public 方法可被任意物件呼叫。預設情況下,方法都是 public 的,除了 initialize 方法總是 private 的。

  Private 方法: Private 方法不能從類外部訪問或檢視。只有類方法可以訪問私有成員。

  Protected 方法: Protected 方法只能被類及其子類的物件呼叫。訪問也只能在類及其子類內部進行。

  下面是一個簡單的例項,演示了這三種修飾符的語法:

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 例項方法預設是 public 的

  def getArea

  getWidth() * getHeight

  end

  # 定義 private 的訪問器方法

  def getWidth

  @width

  end

  def getHeight

  @height

  end

  # make them private

  private :getWidth, :getHeight

  # 用於輸出面積的例項方法

  def printArea

  @area = getWidth() * getHeight

  puts "Big box area is : #@area"

  end

  # 讓例項方法是 protected 的

  protected :printArea

  end

  # 建立物件

  box = Box.new(10, 20)

  # 呼叫例項方法

  a = box.getArea()

  puts "Area of the box is : #{a}"

  # 嘗試呼叫 protected 的例項方法

  box.printArea()

  當上面的程式碼執行時,它會產生以下結果。在這裡,第一種方法呼叫成功,但是第二方法會產生一個問題。

  Area of the box is : 200

  test.rb:42: protected method `printArea' called for #

   (NoMethodError)

  類的繼承

  繼承,是面向物件程式設計中最重要的概念之一。繼承允許我們根據另一個類定義一個類,這樣使得建立和維護應用程式變得更加容易。

  繼承有助於重用程式碼和快速執行,不幸的是,Ruby 不支援多繼承,但是 Ruby 支援 mixins。mixin 就像是多繼承的一個特定實現,在多繼承中,只有介面部分是可繼承的。

  當建立類時,程式設計師可以直接指定新類繼承自某個已有類的成員,這樣就不用從頭編寫新的資料成員和成員函式。這個已有類被稱為基類或父類,新類被稱為派生類或子類。

  Ruby 也提供了子類化的概念,子類化即繼承,下面的例項解釋了這個概念。擴充套件一個類的語法非常簡單。只要新增一個 < 字元和父類的名稱到類語句中即可。例如,下面定義了類 BigBox 是 Box 的子類:

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 例項方法

  def getArea

  @width * @height

  end

  end

  # 定義子類

  class BigBox < Box

  # 新增一個新的例項方法

  def printArea

  @area = @width * @height

  puts "Big box area is : #@area"

  end

  end

  # 建立物件

  box = BigBox.new(10, 20)

  # 輸出面積

  box.printArea()

  當上面的程式碼執行時,它會產生以下結果:

  Big box area is : 200

  方法過載

  雖然您可以在派生類中新增新的功能,但有時您可能想要改變已經在父類中定義的方法的行為。這時您可以保持方法名稱不變,過載方法的功能即可,如下面例項所示:

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 例項方法

  def getArea

  @width * @height

  end

  end

  # 定義子類

  class BigBox < Box

  # 改變已有的 getArea 方法

  def getArea

  @area = @width * @height

  puts "Big box area is : #@area"

  end

  end

  # 建立物件

  box = BigBox.new(10, 20)

  # 使用過載的方法輸出面積

  box.getArea()

  運算子過載

  我們希望使用 + 運算子執行兩個 Box 物件的向量加法,使用 * 運算子來把 Box 的 width 和 height 相乘,使用一元運算子 - 對 Box 的 width 和 height 求反。下面是一個帶有數學運算子定義的 Box 類版本:

  class Box

  def initialize(w,h) # 初始化 width 和 height

  @width,@height = w, h

  end

  def +(other) # 定義 + 來執行向量加法

  Box.new(@width + other.width, @height + other.height)

  end

  def [email protected] # 定義一元運算子 - 來對 width 和 height 求反

  Box.new([email protected], [email protected])

  end

  def *(scalar) # 執行標量乘法

  Box.new(@width*scalar, @height*scalar)

  end

  end

  凍結物件

  有時候,我們想要防止物件被改變。在 Object 中,freeze 方法可實現這點,它能有效地把一個物件變成一個常量。任何物件都可以通過呼叫 Object.freeze 進行凍結。凍結物件不能被修改,也就是說,您不能改變它的例項變數。

  您可以使用 Object.frozen? 方法檢查一個給定的物件是否已經被凍結。如果物件已被凍結,該方法將返回 true,否則返回一個 false 值。下面的例項解釋了這個概念:

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 訪問器方法

  def getWidth

  @width

  end

  def getHeight

  @height

  end

  # 設定器方法

  def setWidth=(value)

  @width = value

  end

  def setHeight=(value)

  @height = value

  end

  end

  # 建立物件

  box = Box.new(10, 20)

  # 讓我們凍結該物件

  box.freeze

  if( box.frozen? )

  puts "Box object is frozen object"

  else

  puts "Box object is normal object"

  end

  # 現在嘗試使用設定器方法

  box.setWidth = 30

  box.setHeight = 50

  # 使用訪問器方法

  x = box.getWidth()

  y = box.getHeight()

  puts "Width of the box is : #{x}"

  puts "Height of the box is : #{y}"

  當上面的程式碼執行時,它會產生以下結果:

  Box object is frozen object

  test.rb:20:in `setWidth=': can't modify frozen object (TypeError)

  from test.rb:39

  類常量

  您可以在類的內部定義一個常量,通過把一個直接的數值或字串值賦給一個變數來定義的,常量的定義不需要使用 @ 或 @@。按照慣例,常量的名稱使用大寫。

  一旦常量被定義,您就不能改變它的值,您可以在類的內部直接訪問常量,就像是訪問變數一樣,但是如果您想要在類的外部訪問常量,那麼您必須使用 classname::constant,如下面例項所示。

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  BOX_COMPANY = "TATA Inc"

  BOXWEIGHT = 10

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 例項方法

  def getArea

  @width * @height

  end

  end

  # 建立物件

  box = Box.new(10, 20)

  # 呼叫例項方法

  a = box.getArea()

  puts "Area of the box is : #{a}"

  puts Box::BOX_COMPANY

  puts "Box weight is: #{Box::BOXWEIGHT}"

  當上面的程式碼執行時,它會產生以下結果:

  Area of the box is : 200

  TATA Inc

  Box weight is: 10

  類常量可被繼承,也可像例項方法一樣被過載。

  使用 allocate 建立物件

  可能有一種情況,您想要在不呼叫物件構造器 initialize 的情況下建立物件,即,使用 new 方法建立物件,在這種情況下,您可以呼叫 allocate 來建立一個未初始化的物件,如下面例項所示:

  #!/usr/bin/ruby -w

  # 定義類

  class Box

  attr_accessor :width, :height

  # 構造器方法

  def initialize(w,h)

  @width, @height = w, h

  end

  # 例項方法

  def getArea

  @width * @height

  end

  end

  # 使用 new 建立物件

  box1 = Box.new(10, 20)

  # 使用 allocate 建立兩一個物件

  box2 = Box.allocate

  # 使用 box1 呼叫例項方法

  a = box1.getArea()

  puts "Area of the box is : #{a}"

  # 使用 box2 呼叫例項方法

  a = box2.getArea()

  puts "Area of the box is : #{a}"

  當上面的程式碼執行時,它會產生以下結果:

  Area of the box is : 200

  test.rb:14: warning: instance variable @width not initialized

  test.rb:14: warning: instance variable @height not initialized

  test.rb:14:in `getArea': undefined method `*'

  for nil:NilClass (NoMethodError) from test.rb:29

  類資訊

  如果類定義是可執行程式碼,這意味著,它們可在某個物件的上下文中執行,self 必須引用一些東西。讓我們來看看下面的例項:.

  #!/usr/bin/ruby -w

  class Box

  # 輸出類資訊

  puts "Type of self = #{self.type}"

  puts "Name of self = #{self.name}"

  end

  當上面的程式碼執行時,它會產生以下結果:

  Type of self = Class

  Name of self = Box

  這意味著類定義可通過把該類作為當前物件來執行,同時也意味著元類和父類中的該方法在方法定義執行期間是可用的。(編輯:雷林鵬 來源:網路)