Ruby模块与类

Ruby是单一继承,一个类只能有一个直接的母类
Mix-in 模式来处理复合需求
多重包含: 由下往上回溯,重复的只看最上方的
     换句话说,程序由上往下执行,由具体到抽象,第二次发现同一个模块就无视
 
 
类定义
 class class-name
  ...
 end
注:  实例变量开头@
      类变量开头@@
      包含模块用 include module-name
      attr_reader, attr_writer, attr_accessor
查看是否包括模块: class-name.include?(module-name)
直接母类               class-name.superclass()
所有祖先               class-name.ancestors()
类方法(静态方法):
      class << class-name
         def func-name
             ...
         end
      end
  或者     
      def class-name.func-name
          ...
      end
-----------------------------------------------------------------------------
 
模块定义
 module module-name
  ...
 end
 
注:  不能直接包含数据(外部无法直接获取)
-----------------------------------------------------------------------------
继承关系(inheritance)
如果类包含了模块,则模块作为”母类“,在真实”母类“后面
母类------模块------当前类
class class-name < supclass-name
 
 

 

posted @ 2017-05-18 14:23  懒虫哥哥  阅读(186)  评论(0编辑  收藏  举报