介紹Ruby中的模組與混合類型的相關知識

來源:互聯網
上載者:User

介紹Ruby中的模組與混合類型的相關知識

   這篇文章主要介紹了Ruby中的模組與混合類型的相關知識,包括平常人們常說的多態與繼承等相關知識點,需要的朋友可以參考下

  模組是組合在一起的方法,類和常量。模組兩個主要好處:

  模組提供了一個命名空間,並避免名稱衝突。

  模組實現混合工廠。

  模組定義了一個命名空間,一個沙箱中方法和常量可以自由使用,而不必擔心踩到其他的方法和常數。

  文法:

  ?

1

2

3

4

5

module Identifier

statement1

statement2

...........

end

  就像被命名為類常量模組中的常量,首字母大寫。定義的方法看起來很相似,模組定義方法就像類的方法。

  調用一個模組方法和類方法一樣,通過模組的名稱它名字前,引用一個常數使用該模組的名稱和兩個冒號。

  例子:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

#!/usr/bin/ruby

 

# Module defined in trig.rb file

 

module Trig

PI = 3.141592654

def Trig.sin(x)

# ..

end

def Trig.cos(x)

# ..

end

end

  我們可以定義一個函數名相同,但在不同的功能模組:

  ?

1

2

3

4

5

6

7

8

9

10

11

#!/usr/bin/ruby

 

# Module defined in moral.rb file

 

module Moral

VERY_BAD = 0

BAD = 1

def Moral.sin(badness)

# ...

end

end

  和類的方法一樣,當在一個模組中定義的方法,指定模組名稱後面跟著一個點,那麼該方法的名稱。

  Ruby require 語句:

  require 語句聲明的是類似於 C/C++ 的 include語句 和 Java 的 import 語句。如果有第三個程式要使用任何定義的模組,它可以簡單地使用Ruby require 語句載入的模組檔案:

  文法:

  require filename

  在這裡,它不是必需的 .rb 檔案名稱擴充。

  例如:

  ?

1

2

3

4

5

require 'trig.rb'

require 'moral'

 

y = Trig.sin(Trig::PI/4)

wrongdoing = Moral.sin(Moral::VERY_BAD)

  重要: 在這裡,這兩個檔案都包含相同的函數名。因此,這將導致在代碼中的歧義,同時包括在調用程式,但的模組避免這個代碼模糊,我們能夠調用適當的功能模組的名稱。

  Ruby include 語句:

  可以嵌入在一個類別模組。要在一個類中嵌入模組,可以使用類中 include 語句:

  文法:

  include modulename

  如果一個模組被定義在單獨的檔案,那麼它需要包含該檔案需要隱藏於公開的模組在一個類的 require 語句之前。

  例子:

  考慮以下模組寫在support.rb檔案。

  ?

1

2

3

4

5

6

7

8

9

module Week

FIRST_DAY = "Sunday"

def Week.weeks_in_month

puts "You have four weeks in a month"

end

def Week.weeks_in_year

puts "You have 52 weeks in a year"

end

end

  現在,可以在如下一類包括這個模組:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#!/usr/bin/ruby

require "support"

 

class Decade

include Week

no_of_yrs=10

def no_of_months

puts Week::FIRST_DAY

number=10*12

puts number

end

end

d1=Decade.new

puts Week::FIRST_DAY

Week.weeks_in_month

Week.weeks_in_year

d1.no_of_months

  這將產生以下結果:

  ?

1

2

3

4

5

Sunday

You have four weeks in a month

You have 52 weeks in a year

Sunday

120

  Ruby中混合類型:

  通過本節之前,假設有物件導向的概念和知識。

  當一個類可以從多個父類繼承的特點,類應該顯示多重繼承。

  Ruby沒有直接中suppoprt多繼承,但Ruby的模組有另一個精彩使用。他們幾乎消除多重繼承的需要,提供了一個工廠稱為混入。

  混合類型給一個精彩的控制方式增加功能類。在代碼中混合類,使用它的代碼能進行進行互動。

  讓我們來看看下面的範例程式碼來獲得混合類型瞭解:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

module A

def a1

end

def a2

end

end

module B

def b1

end

def b2

end

end

 

class Sample

include A

include B

def s1

end

end

 

samp=Sample.new

samp.a1

samp.a2

samp.b1

samp.b2

samp.s1

  模組A包括一種方法,a1和a2。模組B包括一種方法,b1和b2。類樣本包括兩個模組A和B類的樣品可以訪問所有四種方法,即a1, a2, b1 或 b2。因此,可以看到這個類繼承自兩個模組樣品。因此,可以說類的樣本顯示了多重繼承或混入。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.