`
- 浏览:
15924 次
- 性别:
- 来自:
北京
-
"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 代码
- class Test
- @@cla_var = 1
- @inst_var = 1
-
- def self.cla_var
- @@cla_var
- end
- def cla_var
- @@cla_var
- end
- def self.inst_var
- @inst_var
- end
-
- def inst_var
-
- @inst_var
- end
- 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 代码
- class Parent
- @@cla_var = 1
- @inst_var = 2
-
- def self.cla_var
- @@cla_var
- end
- def self.inst_var
- @inst_var
- end
-
- def self.cla_var=(cla_var)
- @@cla_var = cla_var
- end
- def self.inst_var=(inst_var)
- @inst_var = inst_var
- end
-
- end
- class Child1 < Parent
- end
- class Child2 < Parent
- end
-
-
- >> Parent.cla_var
- => 1
- >> Child1.cla_var
- => 1
- >> Child2.cla_var
- => 1
- >> Child1.cla_var = 3
- => 3
- >> Parent.cla_var
- => 3
- >> Child2.cla_var
- => 3
-
-
- >> Parent.inst_var
- => 2
- >> Child1.inst_var
- => nil
- >> Child2.inst_var>> Child1.inst_var = 4
- => 4
- >> Parent.inst_var
- => 2
- >> Child2.inst_var
- => nil
-
- => nil
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
Object#instance_variable_get Object#instance_variable_set 但请注意,这些都共享一个公共前缀 - instance_variable_或class_variable_ 。 这感觉有点,让我们尝试用一些Variable对象来它!安装 gem install ...
-a show instance variable offsets -A show implementation addresses --arch <arch> choose a specific architecture from a universal binary (ppc, ppc64, i386, x86_64) -C <regex> only display classes ...
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...
Java基础学习第一篇: 其中包括Java里面类、变量的...还有关于variables里面instance variable、class variable、local variable和parameter的具体区分和学习。最后是一部分关于Java里面的Literals和packages的知识。
类转储 class-dump是一个命令行实用程序,用于检查Mach-O文件的Objective-C段。 它为类,类别和协议生成声明。 这是使用'otool -ov'提供的相同信息,但以普通的Objective-C... -a show instance variable offsets -A
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. ...
基本的 Java 类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也表示为 Class 对象; 注意 : 1、 Class类 和它的实例的产生: Class的对象是已经存在的类型, 所以不...
@开头:实例变量(Instance variable)。 @@开头:类变量(Class variable)类变量被共享在整个继承链中 大写字母开头:常数(Constant)。 Ruby 全局变量 全局变量以 $ 开头。未初始化的全局变量的值为 nil,在...
Comments Comment Style File Comments Class Comments Function Comments Variable Comments Implementation Comments Punctuation, Spelling and Grammar TODO Comments Deprecation Comments Formatting Line ...
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 ...
public class Example {// Example instance variable :: prefix '_' private int _maVariable; /** * Gets something */ public int GetSomething () { return _maVariable; } /** * Sets Something : ...
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语言里的变量分以下4类: 1. Instance Variables: (Non-Static Fields) 是类里非静态的field ...而不是field的变量叫variable,对应上面的3和4,或者说局部的都是variable。 命名 Java的文档中明
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。 产品特点 根据上下文正确限制静态方法或标准方法(仅显示带有::静态方法,仅...识别$instance = new Class; 类实例化 识别$instance = Class::getInstance(); 单例实例 识别$date =
静态变量、常量和方法学习目标理解实例成员与静态成员会...2、有static修饰的变量/方法,称为静态变量/方法3、没有static修饰变量/方法,称为实例变量/方法(Instance variable/Instance method)实例变量(非static)
快速方法源 存储库: : API说明: : 描述Fast Method Source 是一个 Ruby 库,用于查询方法、过程和 lambda 的源代码和...(self.class) @hash.update(enum.instance_variable_get(:@hash)) else do_with_enum(enum) {
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#语言结构和使用的文档,想做高级独立资深开发的不可错误,另外,此文档为英文原文版。建议作为平时学习阅读之用 目录: Table of Contents 1. Introduction 1 1.1 Hello world 1 1.2 ...