`
qzriso
  • 浏览: 237859 次
  • 性别: Icon_minigender_1
  • 来自: ph
社区版块
存档分类
最新评论

Object and Reflection

阅读更多

摘自: http://anw.stikipad.com/ocean/show/Object+and+Reflection

 

  • 看看 ruby 如何深入探討 object 的相關功能, “o” 表示某個 object instance
    • o.class 找到 object 的 class
    • o.methods 可以找到 methods
    • o.respond_to? 測試某個 method 是否存在
    • o.object_id 它是 object 的唯一 id
    • o.inspect 可以 dump object 內容
    • o.is_a?, o.kind_of?
      當物件的 class 是某個 class 或是某個父字輩 class 或某個 mix-in 模組?
    • o.instance_of?
      當物件的 class 是某個 class?
      ps. 這個好像跟 java 不一樣!
    • o.nil? 是否為 null object?
    • 若你還有進一步需要, 這裡有更多 methods 可以幫你, 這些是將 “Module” mix-in 進去, 可不會在 Object 上找到!
      • private_instance_methods
      • protected_instance_methods
      • public_instance_methods
      • private_class_methods
      • protected_class_methods
      • public_class_methods
      • singleton_methods
      • class_variables
      • ...
        
        no = 100
        puts no.class
        puts no.class.superclass
        
        puts no.is_a? Integer
        puts no.kind_of? Integer
        puts no.instance_of? Integer
        
        
        
        
        
  • 所有的 methods, 在 ruby 都可以用 send 呼叫
    
    "John Coltrane".send(:length)
    "Miles Davis".send("sub", /iles/, '.')
    
    
    
    
    
    • 這是 Proxy 的例子, 利用 ruby 在找不到 method 時一定會試著呼叫 method_missing 的機制就可完成.
      
      class Proxy
        def initialize(object)
          @object = object
        end
      
        def method_missing(symbol, *args)
          @object.send(symbol, *args)
        end
      end
      
      data = ["a", "b", "c"]
      proxy = Proxy.new(data)
      puts proxy.first # Outputs: "a" 
      
      
      
      
      
  • class 的繼承關係? 可以利用 Class#superclass 找到父親. 但 ruby 還可以利用 mix-in 功能將別的模組的變數及 methods 加進去. 這時你就可以用 Module#ancestors 找到父親及 mix-in 模組.
    
    a = 123
    klass = a.class
    while klass do
      print klass
      klass = klass.superclass
      print " < " if  klass
    end
    
    
    
    
    
  • 因為 class 也是 constant, 所以你可以用 Module#constants 找到所有的 class(可能會有點多)
    
    Module.constants.each { |x|
      puts x
    }
    
    
    
    
    
  • 查看所有活著的物件, 要用 ObjectSpace.each_object
    
    class Book
      attr_accessor :title
    end
    
    class Magzine < Book
    end
    
    b1 = Book.new
    b1.title ="A" 
    b2 = Book.new
    b2.title ="B" 
    m1 = Magzine.new
    m1.title ="C" 
    
    ObjectSpace.each_object(Book) { |x|
      puts x.inspect
    }
    
    
    
    
分享到:
评论

相关推荐

    Object Oriented.PHP - Concepts Techniques and Code

    Chapter 14: Creating Documentation Using the Reflection Classes Chapter 15: Extending SQLite Chapter 16: Using PDO Appendix A: Setting Up PHP 5 Appendix B: Conversion Table: PHP 4 and PHP 5 ...

    Object Pascal Handbook,最新Ddelphi书籍,for XE7

    Object Pascal Handbook This is the book summary Table of Contents: Part I Chapter 1: Coding in Pascal ...Chapter 16: Reflection and Attributes Chapter 17: The TObject Class Chapter 18: RunTime Library

    PHP Objects, Patterns, and Practice, 4th Edition

    This edition introduces new object relevant features such as traits, reflection extension additions, callable type hinting, improvements to exception handling, and many smaller language enhancements....

    Pro C# 7: With .NET and .NET Core

    Gain a solid foundation in object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL ...

    C# 6.0 and the .NET 4.6 Framework

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    Csharp.6.0.and.the.NET.4.6.Framework.7th.Edition.1484213335.epub

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    PHP Objects, Patterns, and Practice(Apress,2016)

    It introduces key topics including class declaration, inheritance, reflection and much more. The next section is devoted to design patterns. It explains the principles that make patterns powerful. ...

    C# 6.0 and the .NET 4.6 Framework(7th).pdf 2016第7版pdf

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    C#6.0 and the .NET 5 Framework(Apress,7ed,2016)

    Readers will gain a solid foundation of object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such ...

    Professional C# 7 and .NET Core 2.0

    Chapter 16 Reflection, Metadata, And Dynamic Programming Chapter 17 Managed And Unmanaged Memory Chapter 18 Visual Studio 2017 PART II: .NET Core and the Windows Runtime Chapter 19 Libraries, ...

    The.Dart.Programming.Language.03219277

    Dart is a class-based, object-oriented language that simplifies the development of structured modern apps, scales from small scripts to large applications, and can be compiled to JavaScript for use in...

    The Dart Programming Language

    Dart’s object model, in which everything is an object, even numbers and Boolean values How Dart programs are organized into modular libraries How Dart functions are structured, stored in variables, ...

    Learning ECMAScript 6(PACKT,2015)

    Moving on, it will teach you how to create reflection objects, use it to expose hidden object properties, and test the security of these objects. Next, the book provides use case scenarios of meta ...

    Pro C# 7, 8th Edition

    Gain a solid foundation in object-oriented development techniques, attributes and reflection, generics and collections as well as numerous advanced topics not found in other texts (such as CIL ...

    Ajax for Web Application Developers(Ajax网站开发)

    Data Reflection Pattern An Overview Creating the Pattern Chapter 18. Interaction Patterns Creating a History with Cookies Drag and Drop Chapter 19. Usability Patterns Handling Feedback,...

    Agile Java Crafting Code with Test-Driven Development

    Contains detailed chapters on exceptions and logging, math, I/O, reflection, multithreading, and Swing Offers seamlessly-integrated explanations of Java 5.0's key innovations, from generics to ...

    .Mastering.Dart.1783989564

    Starting with a discussion about the basic features of Dart, we will dive into the more complicated concepts such as generics, annotation with reflection, errors and exceptions, which will help us ...

    Java-Reflection-and-Object-Comparison-in-Java:Java Reflection创建适当的对象,覆盖equal和hashCode方法

    Java中的Java反射和对象比较 项目介绍 首先,通过以下方式设计Java类: 2个私有数据成员int IntValue; 字符串StringValue; 空构造函数定义公共方法void setIntValue(int iIn){...}定义公共方法void ...

Global site tag (gtag.js) - Google Analytics