在
Matz
的一篇
PPT
“Object-Oriented scripting in Ruby”中,
Matz
提到
Ruby
提供一种语言机制
Mix-in
,在其
PPT
中如是描述的“
No Multiple Inheritance
,
but Mix-in
”、“
Mix-in is as strong as multiple inheritance
,
but simple
”。
“module” in Ruby
在
C++
、
Java
、
C#
中都有
namespace
的概念,是用来隔离代码,防止代码冲突的。在
Ruby
中是采用“
module
”这个关键字来完成
namespace
功能的。
//considering the following code:
//MicrosoftCompiler.rb
module Microsoft
def
Microsoft.compile
print "This is microsoft's compiler", "/n"
end
end
//SunCompiler.rb
module Sun
def
Sun.compile
print "This is Sun's compiler", "/n"
end
end
//TestCompiler.rb
require "D://Ruby//MicrosoftCompiler.rb"
require "D://Ruby//SunCompiler.rb"
Microsoft.compile
Sun.compile
//output:
>ruby TestComplier.rb
This is microsoft's compiler
This is Sun's compiler
>Exit code: 0
module
除了体现
namespace
的概念外,从上面代码中我们还可以看到在两个
module
中定义的
method
和调用这个
method
时都在其前面加上其所在
module
的名字了。这样定义的函数叫
module method
。但是不是所有要使用
module
中函数都要显式的加上
module
名字呢?答案:不是。请往下看。
Mix-in
:
Another wonderful use of “module”
由
module method
的定义和调用方式让我们联想到
class method
(注意不是
class instance method
),也让我们从
module
联想到
class
了。但是
A module
是不能像
class
那样拥有实例的,
module
毕竟还不是
class
,但是我们可以在
class
的定义中“
include
”一个
module
,这样会发生什么呢?
//considering the following code:
module Mammal
#
哺乳动物
def suckle
#
哺乳
print "I can suckle my baby" ,"/n"
end
end
module Flyable
#
可飞行的
def fly
#
飞行
print "I can fly", "/n"
end
end
class Chiropter
#
蝙蝠
include Mammal
#
蝙蝠是哺乳动物
include Flying
#
蝙蝠可以飞行
end
aChiropter = Chiropter.new
aChiropter.suckle
aChiropter.fly
//output
:
>ruby TestMixin.rb
I can suckle my baby
I can fly
>Exit code: 0
如上面代码得出的结果我们可以看出,一旦一个
class
中
include
了一个
module,
那么所有
module
中的
methods
就好像成为
class’s instance methods
一样,这就是
mix-in
,看起来被
mix-in
的
modules
的行为更像是这个
class
的
super class
,通过这种方法我们可以间接实现多重继承。但是注意看看被
mixin
的
module
中的函数是如何定义的?和前面代表
namespace
概念的
module
中的
method
定义不同的是在于定义时
method name
前不见了
module name
,这样定义出来的
method
叫
module instance method
,
虽然
module
不能实例化,但是一旦被
mixin
到某个
class
中,这些
module instance method
的使用就等价于
class instance method
了。
总结:
1.
Module
的两种用法
Ø
体现
namespace
概念;
Ø
被
mixin
到
class
中间接的实现类的多重继承。
2.
Module
中的两种
method
定义和用法
module module_name
def
module_name.
module_method_name
# module method , for representing namespace concept
end
def module_instance_method_name
# module instance method
# Once the module has been mixed in,
#the method will be equal to a class instance method
end
end
分享到:
相关推荐
在微信小程序的开发中,`mixin` 是一种常见的代码复用机制,它允许开发者定义一组通用的方法或者属性,然后将这些通用部分混入(mix in)到不同的页面(Page)或组件(Component)中,避免了代码重复,提高了代码的...
Mixin框架是Java编程语言中的一个开源项目,它利用ASM库来实现对类的低级别修改,也就是所谓的"混合"(Mixins)。这个框架的核心概念是将功能或行为注入到目标类中,而无需继承或者使用代理模式。在游戏开发、模块化...
【ymixin:阅文前端团队的CSS预处理器mixin库详解】 在前端开发中,CSS预处理器如Sass、Less和Stylus等已经成为提升样式编写效率和代码可维护性的必备工具。ymixin,作为阅文前端团队打造的一款CSS预处理器mixin库...
"react-addons-pure-render-mixin"是React的一个官方加载项,它提供了一个优化性能的策略,特别是在处理大型复杂应用时。 标题中的“react-addons-pure-render-mixin”是React的一个关键特性,用于帮助开发者实现更...
A Swift mixin for UITableViewCells and UICollectionViewCells.zip,A Swift mixin for reusing views easily and in a type-safe way (UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers...
modernizr-mixin, 在Sass中,针对测试的简单而全面的mixin hardwarebutton混合 一种简单的DRYier测试方法,在Sass中更快更。安装要求 ruby 3.4或者 LibSass 3.2Libsass警告:在 Libsass 3.2.3中有一个已知 Bug,它...
资源分类:Python库 所属语言:Python 资源全名:colcon_mixin-0.1.3-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Vue 中的 Mixin 及其应用场景 在 Vue 中,Mixin 是一种提供了方法实现的类,其他类可以访问 Mixin 类的方法而不必成为其子类。Mixin 类通常作为功能模块使用,在需要该功能时“混入”,有利于代码复用又避免了多...
小程序mixin实现wx-mixin小程序扩展。包含mixin支持,事件勾子支持,从某个小程序页面直接调用完成页面其他方法跨页面通信。小程序mixin使用import createPage from '../../utils/createPage';import sayHello from ...
mixin是一个闪电交易快速的点点对的数字交道项目,它拥有非常好的技术栈。
本文详细探讨了组件的混入(mixin)机制。首先介绍了混入机制的基本概念和产生背景,接着深入剖析其工作原理,包括属性合并、方法合并等细节。通过丰富的代码示例展示了在不同场景下混入的实际应用,如代码复用、...
在Vue.js中,`mixin`(混入)是一个非常有用的工具,它允许开发者定义可复用的行为,这些行为可以被多个组件共享。下面将详细探讨Vue mixin混入配置的学习,包括其工作原理、使用场景、优点以及如何在实际项目中应用...
It's a good start to understand how to write mixin in Object-C. ### Import ObjCMixin ```objc #import ``` ### Define and implement a module Declare a module. ```objc @module(MyModule) @property...
而Mixin是Java编程语言中的一种强大的代码注入框架,常用于Minecraft模组开发,它允许开发者无侵入地修改游戏的内部行为,而无需直接继承或修改原代码。本示例项目"forge-mixin-example"就是针对Minecraft Forge ...
"event-emitter-mixin"是一个专为前端设计的开源库,它实现了事件发射器(Event Emitter)的混合功能,并且采用了ES7的装饰器(Decorator)语法,使得代码更加简洁和易读。 事件发射器(Event Emitter)是软件工程...
Python库pickle-mixin-1.0.2是一个用于序列化和反序列化的工具,它扩展了Python内置的pickle模块。pickle模块是Python中用于将对象的状态转化为可存储或可传输格式,以及从这种格式恢复对象的原状的工具。这个库的...
### 应用笔记LAT1206+TouchGFX+控件附加Mixin功能的方法介绍 #### 1. 引言 随着嵌入式系统技术的发展,触摸屏的应用越来越广泛,而用户界面的设计变得尤为重要。TouchGFX作为一种专用于STM32微控制器的图形用户...
Mixin是将一个对象的属性和方法混入到另一个对象中的一种模式,它允许我们把一个类的某些功能复制到另一个类中,而无需建立真正的继承关系。 首先,我们看到一个augment函数的实现,它的目的是为了能够将一个类...
【前端项目-universal-mixin.zip】是一个针对前端开发的压缩包,其主要目的是提供一个跨JavaScript引擎的混合(mixin)解决方案,使开发者能够在旧的、现代的以及未来的JavaScript环境中编写兼容性强、轻量级的特性...
混合Mixin BFT-DAG网络参考实现,受信任的执行环境尚未集成到此存储库中。开始使用按照此指南安装golang并设置GOPATH 。 $ git clone https://github.com/MixinNetwork/mixin.git$ cd mixin$ go build mixin命令既是...