美文网首页
Ruby 模块

Ruby 模块

作者: changsanjiang | 来源:发表于2017-09-23 21:10 被阅读5次
    
    # 模块
        # 模块(Module)是一种把方法、类和常量组合在一起的方式. 
        # 模块提供了一个命名空间, 避免名字冲突.
        # 模块实现了 mixin 装置.
    
        # 模块类似于类, 但有以下不同:
        # => 模块不能实例化
        # => 模块没有子类
        # => 模块只能被另一个模块定义
        # => 可以定义多个函数名称相同, 但是功能不同的模块
    
    
    # $LOAD_PATH << '.'
    
    # require "Module2.rb"
    
    module Module1
        CONST = 100
        def Module1.my_print
            puts CONST
        end
    
        def test1
            puts "..."
        end
    end
    
    
    
    class Person
    
    include Module1
        def Person.test()
            Module1::my_print
        end
    end
    
    Person.test
    Person.new.test1
    
    

    相关文章

      网友评论

          本文标题:Ruby 模块

          本文链接:https://www.haomeiwen.com/subject/szhhextx.html