2014年10月2日 星期四

Ruby 中 class 與 module 的差異.

來源: http://stackoverflow.com/questions/151505/difference-between-a-class-and-a-module

想像一下~  module 就像是 library , 可以被其他 classs include or extend .


╔═══════════════╦═══════════════════════════╦═════════════════════════════════╗
                class                      module                          
╠═══════════════╬═══════════════════════════╬═════════════════════════════════╣
 instantiation  can be instantiated        can *not* be instantiated       
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
 usage          object creation            mixin facility. provide         
                                             a namespace.                  
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
 superclass     module                     object                          
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
 consists of    methods, constants,        methods, constants,             
                  and variables              and classes                   
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
 methods        class methods,             module methods,                 
                  instance methods           instance methods              
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
 inheritance    inherits behavior and can  No inheritance                  
                  be base for inheritance                                  
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
 inclusion      cannot be included         can be included in classes and  
                                             modules by using the include  
                                             command (includes all         
                                             instance methods as instance  
                                             methods in class/module)      
╟───────────────╫───────────────────────────╫─────────────────────────────────╢
 extension      can not extend with        module can extend instance by   
                  extend command             using extend command (extends 
                  (only with inheritance)    given instance with singleton 
                                             methods from module)          
╚═══════════════╩═══════════════════════════╩═════════════════════════════════╝

【下列文章您可能也有興趣】

沒有留言: