zoukankan      html  css  js  c++  java
  • Ruby:对象模型(又称八卦模型)笔记

    备注

    如果说哪门语言对我影响最大,那就是Ruby了,在.NET阵营多年,试图去用C#的思维去解释很多东西,当然解释Java是足够了,可惜我也用了好几年去解释Javascript,结果是可想而知的:解释不通。直到我遇到Ruby,这让我了解到一件事:不能用一种语言的思维去解释另外一种语言,当然某些局部特点是有可比性的。

    本文重点解释一下Ruby的对象模型(可以理解我Javascript的原型模型),有此可以看出Ruby的完美和统一。

    对象模型

    无图无真相

    基本规则

    1. 一切皆为对象:instance是一般对象、#instance是临时类型对象、Child是一般类型对象、Class是特殊的元类型对象(其实例是一般类型对象)、Mixinable是一般模块对象、Module是元模块对象(其实例是一般模块对象
    2. 只有实例方法,即方法只能定义在类型上(不像Javascript,可以存储在对象上),即:如果 xxx.class = Class or xxx.class = Module 那么 xxx 可以定义实例方法。
    3. 所有实例变量只存储在对象自身上(不像Javascript,可以存储在原型链的任何位置)。
    4. 所有类型都可以在任何时候进行修改(Open Class)。
    5. instance.method的查找规则:先查看singleton class,然后递归遍历所有super class和Mixed Module,遇到第一个即返回,这个过程也叫:右移一步,然后向上。

    代码示例

     1 # coding: utf-8
     2 
     3 class Parent
     4     def hi()
     5         puts "hi"
     6     end
     7 end
     8 
     9 module Mixinable
    10     def hey()
    11         puts "hey"
    12     end
    13 end
    14 
    15 class Child < Parent
    16     include Mixinable
    17 
    18     def hello()
    19         puts "hello"
    20     end
    21 end
    22 
    23 instance = Child.new
    24 instance.hello
    25 instance.hey
    26 instance.hi

    如何修改Singleton Class?

    第一种方式

    1 class << instance
    2     def instance_singleton_method_one
    3         puts "instance_singleton_method_one"
    4     end
    5 end
    6 
    7 instance.instance_singleton_method_one

    第二种形式

    1 def instance.instance_singleton_method_two
    2     puts "instance_singleton_method_two"
    3 end
    4 
    5 instance.instance_singleton_method_two

    如何修改类型,如Child?

    注意:下面演示的是“Open Class”,不是重新定义一个类型。

    1 class Child
    2     def child_method_one()
    3         puts "child_method_one"
    4     end
    5 end
    6 
    7 instance.child_method_one

    类型方法是特殊的实例方法,这些方法定义在类型的Singleton Class中。

    第一种方式

    1 class Child
    2     def Child.child_class_method_one()
    3         puts "child_class_method_one"
    4     end
    5 end
    6 
    7 Child.child_class_method_one

    第二种形式

    1 class Child
    2     def self.child_class_method_two()
    3         puts "child_class_method_two"
    4     end
    5 end
    6 
    7 Child.child_class_method_two

    第三种形式

    1 def Child.child_class_method_three
    2     puts "child_class_method_three"
    3 end
    4 
    5 Child.child_class_method_three

    第四种形式

    1 class << Child
    2     def child_class_method_four()
    3         puts "child_class_method_four"
    4     end
    5 end
    6 
    7 Child.child_class_method_four

    备注

    图中很多关系有兴趣的朋友可以自己验证,本文没有涉及元编程,元编程不过是按照一定的元数据来修改类型定义或生成类型定义,也就是说元编程的前提是类型可以动态的修改,了解了本文,元编程不在话下。

  • 相关阅读:
    python字典类型
    python集合类型
    python元组类型
    python列表类型
    Python中的序列操作
    python字符串格式化输出
    python可变对象和不可变对象的解释
    Python数值类型
    Python循环结构用法
    python if条件判断语句
  • 原文地址:https://www.cnblogs.com/happyframework/p/3316063.html
Copyright © 2011-2022 走看看