Day02 - Ruby比一比:Module模块与Class类别

前情提要

在第一天里,我很激昂地用Ruby的类别、物件、方法,写了宣言!

 

class TingIsIronman

def initialize

@message =“I'm going to write 30 IT articles in 30 days!”

end

def method

puts @message.gsub(“write”,“create”)

end

end

 

object = TingIsIronman.new

object.method

#=> I'm going to create 30 IT articles in 30 days!

然后我从探索类别的栗子?:查询class的父类别时,发现一个铁铮铮的事实:tw.class.superclass.class.superclass印出来的结果是moudle。tw.class.superclass.class.superclass.superclass印出来的结果是Object。

 

(白话翻译吐司:

「番薯岛」的类别是「国家」,「国家的」父类别是「世界」;

「世界」的类别,依旧是个「类别」;「类别」的「父类别」,是个「模块」喔喔喔喔!!!)

 

所以在第二天的文章里,我们就来聊一聊模块(Module)吧!:)

 

Ruby经典面试题目#02

类别与模块有什么不同?What's the Difference Between a Class and a Module?

 

从以上「番薯岛」的例子里,我们从「类别」的「父类别」,是个「模块」这句话找到灵感,已经可以做出比较表格的第一列:

 

超级比一比类别Class模块Module

父类别superclass模块Module物件Object

从龙哥的Ruby教学里关于海贼王鲁夫的例子,我得到一个重要的观念:

 

类别可以继承,而模块不行。

 

我们在第一篇文章里已经透过番薯岛的例子利用类别过继承(vmwork),

现在来让我们继续练习继承的概念,new更多「国家」物件,用Ruby语言,领略世界各国之美:

 

class World

def beautiful(scenery)

p“#{scenery} is so beautiful!!“

end

end

 

class Country < World

end

 

tw = Country.new

au = Country.new

jp = Country.new

 

tw.beautiful“Taroko Gorge”

jp.beautiful“Fujisan”

au.beautiful“Great Barrier Reef”

 

程序结果如我们所料:

 

Taroko Gorge is so beautiful!!

Fujisan is so beautiful!!

Great Barrier Reef is so beautiful!!

愿我们都能透过以上继承的代码,看见世界上的每个国家,各自的美好:)

 

说完类别了,来聊模块吧!

 

模块就是像是图书馆(library)。

你到了图书馆借了某本程序书籍,把书本里的「知识」(在程序语言里,我们叫它做方法method),吸收、消化,然后,放入(include)到脑袋。

 

在父母生下我的年代里,Ruby还没有被创造出来(告诉我有哪一个小baby一生下来就会Coding的嘛?没有嘛!),

 

因此,无论是学习Ruby、或是任何程序语言的知识,需要我们后天透过「网际网络」这个世界上最大的图书馆去学习。

所以,我们可以把模块(module)理解为后天的知识,无法被继承(inheritance),而是后天习得的。

 

只要有心,就能学得会:)

 

来动手写一个模块,描述上述的情境:

 

module Library

#网络就像是个大型图书馆模块(名称要以常数、大字英文字母开头,跟书名一样)

 

def IThelp #定义IT邦方法

p“I'm learning from others' IT articles on IThelp Website!”

end

end

 

class EveryoneLearnsRuby

def initialize(name)

@name = name

end

include Library

#让每个人学Ruby时都可以使用Library模块

end

 

Ting = EveryoneLearnsRuby.new(“Ting”)

 

# new一个新人物Ting(OS:大家好我是Ruby新手村民,请多指教!XD)

 

Ting.IThelp

 

#=> I'm learning from others' IT articles on IThelp Website!

 

如果屏幕前面的你也想要学Ruby的话,当然也可以像我一样,使用图书馆模块里的IT邦方法喔!:)

 

You = EveryoneLearnsRuby.new(“You”)

You.IThlep

 

#=> I'm learning from others' IT articles on IThelp Website!

「超级比一比」表格再扩充:

 

超级比一比类别Class模块Module

父类别superclass模块Module物件Object

继承inheritance可继承不可继承

包含inclusion不可被包含可被包含

接下来我们要继续更多探索不同之处。拿The Difference Between a Class and a Module?去请教Google大神,

你会发现出现的第一段文字回答是:

 

The Difference Between a Class and a Module.Basically,a class can be instantiated but a module cannot.A module will never be anything other than a library of methods.A class can be so much more -- it can hold its state(by keeping track of instance variables)and be duplicated as many times as you want.出处

 

这里说明一个很重要的概念instantiation。实体变数(Instance Variables)是在类别class使用时才会被建立,如同我们刚在例子中所举的:

 

class EveryoneLearnsRuby

def initialize(name)

@name = name

end

include Library

#让每个人学Ruby时都可以使用Library模块

end

其中的@name就是实体变数。

 

超级比一比类别Class模块Module(xcsjbj)

父类别superclass模块Module物件Object

继承inheritance可继承不可继承

包含inclusion不可被包含可被包含

延伸extension不可延伸可被延伸

实体化instantiation可被实体化(instantiated)不可被实体化

今天先写到这里,预计明天文章会提到extension。

做这个表格时,我发现:跟别人比较,比得好累好辛苦~

 

还是跟昨天的自己比就好。

只要每天都有进步,都好!:)

转载于:https://www.cnblogs.com/lannyQ-Q/p/10658059.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值