您的位置:首页 > 编程语言 > Ruby

ruby入门_模块

2012-02-29 14:39 225 查看
模块(module)和类同一级

和类类似,但是不能实例化

可以被类包含

self可以指定模块方法

module DemoModel
def foo1 # common method
puts "foo1. common method"
end

def self.foo2 # module method
puts "foo2. module method"
end
end

# here is error
# DemoModel.foo1
DemoModel.foo2

# -------------------------------------

# class with module
class DemoClass
include DemoModel
end

demo = DemoClass.new

demo.foo1

# -------------------------------------

# module with class
module DemoModel1
class DemoClass1
def foo3
puts "foo3."
end
end
end

demo1 = DemoModel1::DemoClass1.new

demo1.foo3


输出

foo2. module method

foo1. common method

foo3.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: