ruby系列教材(15):Class Methods

上一篇 / 下一篇  2008-02-02 15:03:35 / 个人分类:Ruby

Class Methods:work without being tied to any particular object.

使用举例:

class Example

  definstance_method              # instance method

  end

  defExample.class_method     # class method

  end

end

 

定义是用 类名+方法名,应用的例子:

 

class SongList

  MAX_TIME = 5*60                              # 5 minutes,常量,大写,使用下划线分割,我们现在见到的除了类名其他都用下滑线分隔

  def SongList.is_too_long(song)

    return song.duration > MAX_TIME    # return可以去掉,return 返回true or false,这个表达式会赋值给一个临时变量

  end

end

song1 = Song.new("Bicylops", "Fleck", 260)

SongList.is_too_long(song1)→false

song2 = Song.new("The Calling", "Santana", 468)

SongList.is_too_long(song2)→true


TAG: Ruby

 

评分:0

我来说两句

我的栏目

日历

« 2024-05-10  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 22932
  • 日志数: 47
  • 建立时间: 2008-01-29
  • 更新时间: 2008-02-02

RSS订阅

Open Toolbar