`
sean
  • 浏览: 15924 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

"class instance variable" 和 “class variable”的区别

阅读更多
  • class variables are available in class methods and instance methods, but class instance variable is only available in class methods:
    ruby 代码
    1. class Test    
    2.   @@cla_var = 1    
    3.   @inst_var = 1    
    4.        
    5.   def self.cla_var    
    6.     @@cla_var    
    7.   end    
    8.   def cla_var    
    9.    @@cla_var    
    10.   end    
    11.   def self.inst_var    
    12.     @inst_var    
    13.   end    
    14.     
    15.   def inst_var    
    16.      #here @inst_var is treated as the instance variable not class instance variable    
    17.      @inst_var    
    18.   end    
    19.  end    
  • Class variables are shared by children of the class in which they are first defined, but  class instance variables not, every child has it's own copy of class instance variable(Rumor has it that ruby1.9 will change this).
    ruby 代码
     
    1. class Parent  
    2.    @@cla_var = 1  
    3.    @inst_var = 2  
    4.      
    5.     def self.cla_var  
    6.         @@cla_var  
    7.     end  
    8.     def self.inst_var  
    9.         @inst_var  
    10.     end  
    11.   
    12.     def self.cla_var=(cla_var)  
    13.         @@cla_var = cla_var  
    14.     end  
    15.     def self.inst_var=(inst_var)  
    16.         @inst_var = inst_var  
    17.     end  
    18.   
    19. end  
    20. class Child1 < Parent  
    21. end  
    22. class Child2 < Parent  
    23. end  
    24.   
    25. #class variables are shared by parent and all the children  
    26. >> Parent.cla_var  
    27. => 1  
    28. >> Child1.cla_var  
    29. => 1  
    30. >> Child2.cla_var  
    31. => 1  
    32. >> Child1.cla_var = 3  
    33. => 3  
    34. >> Parent.cla_var  
    35. => 3  
    36. >> Child2.cla_var   
    37. => 3  
    38.   
    39. #every children has it's own copy of class instance variables  
    40. >> Parent.inst_var  
    41. => 2  
    42. >> Child1.inst_var  
    43. => nil  
    44. >> Child2.inst_var>> Child1.inst_var = 4  
    45. => 4  
    46. >> Parent.inst_var  
    47. => 2  
    48. >> Child2.inst_var  
    49. => nil  
    50.   
    51. => nil  

分享到:
评论

相关推荐

    variables:类和实例变量的“变量”对象

    Object#instance_variable_get Object#instance_variable_set 但请注意,这些都共享一个公共前缀 - instance_variable_或class_variable_ 。 这感觉有点,让我们尝试用一些Variable对象来它!安装 gem install ...

    Objective-C头文件导出工具class-dump

    -a show instance variable offsets -A show implementation addresses --arch &lt;arch&gt; choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64) -C &lt;regex&gt; only display classes ...

    java api 文档 jdk9

    a - Variable in class java.awt.AWTEventMulticaster A variable in the event chain (listener-a) A - Static variable in class java.awt.PageAttributes.MediaType The MediaType instance for Engineering A, 8...

    Introductory Java.docx

    Java基础学习第一篇: 其中包括Java里面类、变量的...还有关于variables里面instance variable、class variable、local variable和parameter的具体区分和学习。最后是一部分关于Java里面的Literals和packages的知识。

    class-dump:从Mach-O文件生成Objective-C标头

    类转储 class-dump是一个命令行实用程序,用于检查Mach-O文件的Objective-C段。 它为类,类别和协议生成声明。 这是使用'otool -ov'提供的相同信息,但以普通的Objective-C... -a show instance variable offsets -A

    Java 英文习题及答案

    1.The term class variable is a synonym for (a) a private data field (b) a static data field (c) a read-only variable (d) an instance variable 2. Consider the following Java program segment. ...

    day021-反射和注解笔记和代码.rar

    基本的 Java 类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也表示为 Class 对象; 注意 : 1、 Class类 和它的实例的产生: Class的对象是已经存在的类型, 所以不...

    深入分析Ruby 变量

    @开头:实例变量(Instance variable)。 @@开头:类变量(Class variable)类变量被共享在整个继承链中 大写字母开头:常数(Constant)。 Ruby 全局变量 全局变量以 $ 开头。未初始化的全局变量的值为 nil,在...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Comments Comment Style File Comments Class Comments Function Comments Variable Comments Implementation Comments Punctuation, Spelling and Grammar TODO Comments Deprecation Comments Formatting Line ...

    Programming in Objective-C 4th Edition

    Class and Instance Methods 35 The @implementation Section 37 The program Section 39 Accessing Instance Variables and Data Encapsulation 45 Summary 49 Exercises 49 4 Data Types and Expressions 51 Data ...

    UnikornTube_Server

    public class Example {// Example instance variable :: prefix '_' private int _maVariable; /** * Gets something */ public int GetSomething () { return _maVariable; } /** * Sets Something : ...

    servlet2.4doc

    Overview Package Class Tree Deprecated Index Help PREV NEXT FRAMES NO FRAMES A B C D E F G H I J L P R S U V -------------------------------------------------------------------------------- A ...

    Java变量的分类与初始化

    变量  Java语言里的变量分以下4类:  1. Instance Variables: (Non-Static Fields) 是类里非静态的field ...而不是field的变量叫variable,对应上面的3和4,或者说局部的都是variable。  命名  Java的文档中明

    JSP Simple Examples

    At last use the instance of the class to access the methods of the java file. Setting Colors in JSP In Jsp also we can set the background color which we want, the font color can be changed. The ...

    phpcomplete.vim:改进了PHP全功能

    关于 ... 基于默认的phpcomplete.vim。 产品特点 根据上下文正确限制静态方法或标准方法(仅显示带有::静态方法,仅...识别$instance = new Class; 类实例化 识别$instance = Class::getInstance(); 单例实例 识别$date =

    编程技能训练与等级考试辅导:静态变量、常量和方法.pptx

    静态变量、常量和方法学习目标理解实例成员与静态成员会...2、有static修饰的变量/方法,称为静态变量/方法3、没有static修饰变量/方法,称为实例变量/方法(Instance variable/Instance method)实例变量(非static)

    fast_method_source:检索方法和过程的源和注释

    快速方法源 存储库: : API说明: : 描述Fast Method Source 是一个 Ruby 库,用于查询方法、过程和 lambda 的源代码和...(self.class) @hash.update(enum.instance_variable_get(:@hash)) else do_with_enum(enum) {

    Java语言编码规范

    10.1 提供对实例以及类变量的访问控制(PROVIDING ACCESS TO INSTANCE AND CLASS VARIABLES) 20 10.2 引用类变量和类方法(REFERRING TO CLASS VARIABLES AND METHODS) 20 10.3 常量(CONSTANTS) 21 10.4 变量赋值...

    C# 语言规格说明(English Edition第五版)

    微软内部提供的详细描述C#语言结构和使用的文档,想做高级独立资深开发的不可错误,另外,此文档为英文原文版。建议作为平时学习阅读之用 目录: Table of Contents 1. Introduction 1 1.1 Hello world 1 1.2 ...

Global site tag (gtag.js) - Google Analytics