Day04 -玩弄Ruby的方法:instance method与class method

前情提要
在第三天时,我们解说了如何在class里用include与extend,去使用module的method。

Include is for adding methods to an instance of a class.
Extend is for adding class methods.(出处)
…Also,it is sometimes ok to use“include”to add both instance and class methods.#这句话比较进阶,之后再研究:)
并透过图书馆模块的代码明白这段话的含义:

include是把类别中的物件实体加上方法;

extend是用于类别方法。

module Library
def IThelp
p“IThelp helps me!”
end
end

class NewbieLearnsRuby
include Library
end

class ExtendRuby
extend Library
end

NewbieLearnsRuby.new.IThelp
# IThelp helps me!

ExtendRuby.IThelp
# IThelp helps me!
话不多说,进入今天的章节(xcsjbj):

Ruby经典面试题目#04
解释实体方法与类别方法Explain instance method and class method.

类别方法class method
为了了解类别方法,我们今天要建立新的类别class:铁人赛名单IronmanList,让这个class利用find方法,以传入的id值顺利找到某位铁人赛的参赛者:

class IronmanList
class << self
def find(id)
p“finding Ironman ID: #{id}”
end
end
end

IronmanList.find(1)
# finding Ironman ID: 1
当传入1给id,会使IronmanList这个类别,印出finding Ironman ID: 1。

以上的程序代表,当接收者不是物件object,而是类别class本身,就是一个类别方法class method。

这边的<<指的是push方法,用在class method,意思是将self method push到类别class里。
铁人赛名单class也可写为:

class IronmanList
#class << self
def self.find(id)#在这里的self is a class Method
p“finding Ironman ID: #{id}”
end
#end
end

IronmanList.find(1)
我们把class << self…end这部分都用注释消掉,直接使用self这个class method,让self.find(id)与之前呈现出一样的结果!

使用class method的情况

当我们要写class method时,如果此方法并不会和某个特定的实体变数绑在一起,就该使用类别方法!

实体方法(instance method)
把铁人赛名单类别扩充一下,除了find方法,还有ironmanwinner方法:

class IronmanList

def self.find(id)
p“finding Ironman ID: #{id}”
end

def ironmanwinner
p“I've got a trophy!”
end

end

IronmanList.find(1)#这是类别方法
IronmanList.new.ironmanwinner #这是实体方法
结果会印出:

finding Ironman ID: 1
I've got a trophy!
使用instance method的情况

如果你需要将实体方法,运用在某个客制化的实体。

This is often when the functionality concerns the identity of the instance such as calling properties on the object,or invoking behaviour.出处
如同铁人赛的赢家不会只有一个名额,只要能自我挑战成功,都能练成铁人:)。
因此我们可以再new更多的物件,尽情使用这个ironmanwinner实例方法(xyyfl):

class IronmanList

def self.find(id)
p“finding Ironman ID: #{id}”
end


def ironmanwinner
p“I've got a trophy!”
end

end
# IronmanList.find(1)

Ting = IronmanList.new
Ting.ironmanwinner

Bater = IronmanList.new
Bater.ironmanwinner
结果印出:

“I've got a trophy!”
“I've got a trophy!”
例子不会只有一种,解释方法更不会只有一种。我们除了用自己写的代码理解概念,近一步拿关键字instance method class method ruby去请教Google大神透过网络这座大图书馆,其他工程师们的博客文章、透过各种文字说明与举例加深我们的印象。看到排名第一的解释写着:

Class can use methods from three areas:

Instances of Class have access to the instance methods defined in Module
Instances of class can call methods that are defined as instance methods in their class.
Or instances can call a singleton method of a class object.出处
类别可以使用三种方法:

类别的物件实体,去存取模块里的实体方法。(例如:第二天在LibraryModule所写的的IThelp Method)
类别的物件实体,用自己类别里定义的实体方法。(例如:本日第四天的Ironmanlist Method)
类别可以呼叫类别物件的单例方法(singleton method)。(例如:本日第四天的IronmanListClass的self.find(id)Method)
这里又发现一个新名词了:singleton method,这将成为我们下一篇的素材呢!

=欲知详情,下回分解!=

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值