前情提要
在第三天时,我们解说了如何在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,这将成为我们下一篇的素材呢!

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

Day04 -玩弄Ruby的方法:instance method与class method的更多相关文章

  1. 雷林鹏分享:Ruby CGI方法

    Ruby CGI方法 以下为CGI类的方法列表: 序号方法描述 1CGI::new([ level="query"]) 创建 CGI 对象.query可以是以下值: query: ...

  2. Bound Method and Unbound Method - 绑定方法 与 非绑定方法

    Bound Method and Unbound Method 通常有两种方法对类的方法(instance.method)/属性(class.attribute)进行引用, 一种称做 Bound Me ...

  3. [Python] Python 之 function, unbound method 和 bound method

    首先看一下以下示例.(Python 2.7) #!/usr/bin/env python # -*- coding: utf-8 -*- class C(object): def foo(self): ...

  4. 关于.ToList(): LINQ to Entities does not recognize the method ‘xxx’ method, and this method cannot be translated into a store expression.

    LINQ to Entities works by translating LINQ queries to SQL queries, then executing the resulting quer ...

  5. java代码中init method和destroy method的三种使用方式

    在java的实际开发过程中,我们可能常常需要使用到init method和destroy method,比如初始化一个对象(bean)后立即初始化(加载)一些数据,在销毁一个对象之前进行垃圾回收等等. ...

  6. Modified Least Square Method and Ransan Method to Fit Circle from Data

    In OpenCv, it only provide the function fitEllipse to fit Ellipse, but doesn't provide function to f ...

  7. Invalid character found in method name. HTTP method names must be tokens

      o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header Note: further occurrenc ...

  8. SpringBoot:Invalid character found in method name. HTTP method names must be tokens

    问题背景 关于SpringBoot应用挂了很久之后,会发生Invalid character found in method name. HTTP method names must be token ...

  9. tomcat 启动报错 Invalid character found in method name. HTTP method names must be tokens

    解决:Invalid character found in method name. HTTP method names must be tokens   阿里云上弄了一个tomcat,经常半夜发送崩 ...

随机推荐

  1. JSON和JSONP,浅析JSONP解决AJAX跨域问题

    说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...

  2. laravel+Redis简单实现队列通过压力测试的高并发处理

    秒杀活动 在一般的网络商城中我们会经常接触到一些高并发的业务状况,例如我们常见的秒杀抢购等活动, 在这些业务中我们经常需要处理一些关于请求信息过滤以及商品库存的问题. 在请求中比较常见的状况是同一用户 ...

  3. Error configuring application listener of class org.springframework.web.cont

    解决方案 1:   1. 打开工程属性对话框,到Deployment Assembly页面,点击Add   2. 选择Jave Build Path Entries 3. 把程序用于的Library加 ...

  4. win10 家庭版 升级 win10企业版

    更改秘钥 我的电脑(右键)->属性-> 更改产品秘钥 -> 96YNV-9X4RP-2YYKB-RMQH4-6Q72D->重启系统 如果秘钥过期了,就百度按时间搜索,总有一个是 ...

  5. M25P16中文版

    转载:https://blog.csdn.net/weiweiliulu/article/details/23666717 1.M25P16概述     M25P16是一款带有先进写保护机制和高速SP ...

  6. 更新 TeX Live 软件包

    这个 TeX Live 软件,你得时常更新一下,不然会遇到一些由软件包自身 Bug 导致的编译问题.比如,这次我使用 Beamer 软件包写演示文稿,就遇到问题了,结果发现是软件包自身存在的问题.安装 ...

  7. 《深度探索C++对象模型》读书笔记(二)

    第三章:Data语意学 这一章主要讲了类中的数据在内存中是如何分配的,包括(多重)继承和多态. 让我们首先从一段代码开始: class X{}; class Y :virtual public X{} ...

  8. asp微信支付代码v4.1无需证书版,带回调入库的asp支付源码

    昨天帮一个客户写的,他的程序是老的asp,想实现微信在手机上下单付款,让帮忙给写一份asp微信支付的接口代码,昨天晚上闲着没事,搞了一个晚上才搞好,其实asp支付并不需要安装证书,其实asp支付也很好 ...

  9. mysql双机热备的实现

    转:http://blog.csdn.net/qq394829044/article/details/53203645 Mysql数据库没有增量备份的机制,当数据量太大的时候备份是一个很大的问题.还好 ...

  10. 所有设计复杂的ORM都是浮云

    很久没有写文章了. 一直很忙,不是很有时间整理. 今天主要是来吐槽下那些设计很复杂的ORM的. 项目做的越多,越觉得ORM这个东西设计的太复杂实在是没什么意义. 比较推崇Dapper这样比较简单,效率 ...