1.self的使用

2.private方法的使用

3.方法查找

4.方法执行

5.refinement

6.单件类

7.单例方法定义

1.self的使用

#1 当调用一个方法时,接收者会扮演self的角色
#2 类和模块的定义中,并且在任何方法定义之外,self的角色由这个类或模块担任
class MyClass
  puts self      #打印结果是MyClass
end
#3 当前对象显示,在类或者模块外面,当前对象是main
puts self  #这段代码返回的是main

2.private方法的使用

#权限设置中已经说明

3.方法查找

#方法查找规则:向右一步,再向上
#include和prepend的区别:在导入关系中,include在类的正上面,prepend在类的正下面
module One
end

module Two
end

class Demo
  include One
  prepend Two
end
Demo.ancestors #[Two, Demo, One, Object, Kernel, BasicObject]

#include和prepend的执行顺序是先入后出
module Printable
end

module Document
end

class Book
  include Document
  include Printable
end
Book.ancestors #[Book, Printable, Document, Object, Kernel, BasicObject]

class Book
  prepend Document
  prepend Printable
end
Book.ancestors #[Printable, Document, Book, Object, Kernel, BasicObject]

4.方法执行

#对象是一组实例变量和指向类的引用,若方法不执行,则对象中则不包括方法中的实例变量
class Demo
  def one
    @test=1
  end
end
obj = Demo.new
obj.instance_variables #[]
obj.one
obj.instance_variables #[@test]

#方法内部已经定义的方法在外部方法被调用后执行,且指向同一对象
class MyClass
  def testing_self
    my_method  #这段代码的作用和self.my_method一致
  end

  def my_method
    puts "this is my_method"
  end
end

obj = MyClass.new
obj.testing_self  #这段代码返回的是this is my_method

5.refinement

#refine中已经说明,说明了using和refine两个方法 的使用

6.单件类

1.为了补全对象模型的知识,找到单件方法的藏身之所。
2.我们称单件方法所在的类为单件类。
3.单件类是一个特殊的类,它只有一个实例,并且不能被继承(理解?)。
4.一个对象的单件类的父类是这个对象的类。
5.一个类的单件类的超类是这个类的超类的单件类。

#获得eigenclass:
obj =  Object.new
eigenclass = class << obj
  self
end
#通过class << obj的形式可以进入obj单件类的领域中
#这里的self对象就是单件类,整个返回的就是一个单件类
puts eigenclass.class #=>Class


#为了方便查找对象的eigenclass,在Object类中定义了如下的代码:
class Object
  def eigenclass
    class << self
      self
    end
  end
end

#任何类(除了BasicObject)都是继承于Object,因此此方法eigenclass能适用于所有的对象
#任何对象调用eigenclass方法,返回的都是该对象的单件类。

#对象单件方法表示形式:
obj = Object.new
class << obj
  def a_singleton_method
    puts "this is the obj singleton method"
  end
end

obj.a_singleton_method #=>this is the obj singleton method

#演示如何寻找单件类的父类:
class Object
  def eigenclass
    class << self
      self
    end
  end
end

class C
  def a_method
    puts "this is the class C method"
  end
end

class D < C
end

obj = D.new
obj.a_method

class << obj
  def a_singleton_method
    puts "this is the singleton method"
  end
end

#对象单件类的父类是该对象的类
obj.eigenclass.superclass #=>D
对象模型查找方法补充:
1.对象有eigenclass,从这个eigenclass类中开始查找方法。
2.在eigenclass类中找不到方法,那么它会沿着祖先链向上来到eigenclass的超类。

class Object
  def eigenclass
    class << self
      self
    end
  end
end

class C
  class << self
    def a_class_method
      puts "C.a_class_method"
    end
  end
end

class D < C
end

C.eigenclass  #=>#<Class:C>
D.eigenclass  #=>#<Class:D>
D.eigenclass.superclass #=>#<Class:C>
C.eigenclass.superclass  #=>#<Class:Object>

结论:类的单件类的父类是其类的父类的单件类

#D(D类的eigenclass)的超类是#C,#C的超类是#Object,于是可以在子类调用父类的类方法
D.a_class_method #=>"C.a_class_method"

7.单例方法定义

# 定义单例方法的方式
# 1
class A 
  def self.hi 
    p 'hi' 
  end
end

# 2
class A 
  class << self 
    def hello 
      p 'hello' 
    end 
  end
end

# 3
def A.hey 
  p 'hey'
end

# 4
(class << A; self; end).class_eval do 
  def you 
    p 'you' 
  end
end

# 5
A.singleton_class.class_eval do 
  def folk 
    p 'folk' 
  end
end

# 6
A.define_singleton_method(:hello) do |*args| 
  p args
end

results matching ""

    No results matching ""