ruby系列教材(15):Class Methods
来源:百度文库 编辑:神马文学网 时间:2024/07/08 10:33:45
Class Methods:work without being tied to any particular object.
使用举例:
class Example
def instance_method # instance method
end
def Example.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
使用举例:
class Example
def instance_method # instance method
end
def Example.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
ruby系列教材(15):Class Methods
ruby系列教材(14):Class Variables
ruby系列教材(13):Attributes, Instance Variables, and Methods
ruby系列教材(12):Virtual Attributes
ruby系列教材(18):Specifying Access Control
现代秘书系列教材
Meta-Programming in Ruby: 动态生成class,并添加attribute和method。 - AllenYoung - JavaEye技术网站
黑客入门教材
第一节、黑客的种类和行为 黑客入门教材
第一节、黑客的种类和行为 Simulation methods 数字动画基础(数字媒体系列教材新世纪高等学校教材) 关于 Ruby - Ruby 官方网站 国际经济学(对外经济贸易大学远程教育系列教材)章昌裕 手写速记与电子速记(现代秘书系列教材) 刑法(高职高专法律系列教材)目录 Ruby QuickRef Ruby简介 ruby网址 ruby笔记 XPathNavigator Class Methods for Synthesis of Methamphetamine Feature Extraction Methods: A Survey Computer Science Research Methods: Resources Methods for Synthesis of Methamphetamine!
第一节、黑客的种类和行为 黑客入门教材
第一节、黑客的种类和行为 Simulation methods 数字动画基础(数字媒体系列教材新世纪高等学校教材) 关于 Ruby - Ruby 官方网站 国际经济学(对外经济贸易大学远程教育系列教材)章昌裕 手写速记与电子速记(现代秘书系列教材) 刑法(高职高专法律系列教材)目录 Ruby QuickRef Ruby简介 ruby网址 ruby笔记 XPathNavigator Class Methods for Synthesis of Methamphetamine Feature Extraction Methods: A Survey Computer Science Research Methods: Resources Methods for Synthesis of Methamphetamine!