[Ruby笔记]28.Ruby @@class_variables 类变量 vs @instance_variable 实例变量
2016-07-19 23:42
609 查看
@@class_variables
TestCase
想象这样一个场景,一次小班授课,参加的学生有A B C三人 ,这时候老师开始提问了,我们使用类
Student记录 :
到场的学生名单;
每人答题次数;
老师总的提问次数
Code
Class Student 类
@@names存着学生名单的数组 ;
@@answers存着每个学生答题次数的哈希表 ;
@@total_answers存着老师总的提问次数的
class variable;
attr_reader :name存着某个学生名字的
instance variable
class Student @@names = [] # array @@answers = {} # hash @@total_answers = 0 attr_reader :name def self.total_answers @@total_answers end def self.add_student(name) unless @@names.include?(name) puts "Welcome : #{name}" @@names << name # << array - append operator @@answers[name] = 0 end end def initialize(name) if @@names.include?(name) puts "This time : #{name}" @name = name @@answers[name] += 1 @@total_answers += 1 else raise "No such person : #{name}" end end def answer @@answers[self.name] end end
注:关于
@name这里,这是一个
instance variable,具体见笔记17.
到场学生名单
write code
这时候的self自然而言是Student这个类,代码里对应的就是
self.add_student(name),到场学生分别是
ABC三人 :
# self is the class object-Student Student.add_student("A") Student.add_student("B") Student.add_student("C")
run it and output
Welcome : A Welcome : B Welcome : C
开始提问
write code
老师提了3个问题,喊道A同学2次,喊道B同学一次,点名提问真是太刺激了!#self is the instance of Student that's calling the method ask1 = Student.new("A") ask2 = Student.new("B") ask3 = Student.new("A")
run it and output
This time : A This time : B This time : A
记录次数的显示
write code
这时候self就变成每个
ask,可以是
ask1也可以是
ask2或者
ask3;
最有趣的部分就在于此,
aks1以及
ask3这两次问题都是
A来回答的,因此无论是用
ask1.answer还是
ask3.answer都会得到A的答题次数,那就是2次;
puts "A answered question ? times : #{ask1.answer}" # puts "A answered question ? times : #{ask3.answer}" puts "B answered question ? times : #{ask2.answer}" puts " " puts "There are ? total answers : #{Student.total_answers}"
那是因为,回到
class Student的代码里,这时候的
self是某个
ask,但是无论是
aks1还是
ask3,它们的
name都是
A,所以无论是
ask1还是
ask3调用
answer的时候都会得到A的答题次数 :
def answer @@answers[self.name] end
run it and output
A answered question ? times : 2 B answered question ? times : 1 There are ? total answers : 3
如果喊错人名字了呢?
write code
没有名字是D的学生,因此根据实现代码里的raise语句,程序会报错:
ask4 = Student.new("D")
run it and output
stu.rb:25:in `initialize': No such person : D (RuntimeError) from stu.rb:57:in `new' from stu.rb:57:in `<main>'
note
self在做
Student.add_student("A")时,是
Student这个类。
self在做
ask1 = Student.new("A")时,是
ask1这个实例对象。
link
本篇以具体的用例为笔记23的补充,此时回顾温习甚佳。reference
《The Well-Grounded Rubyist, Second Edition》5.2.5. Class variable syntax, scope, and visibility
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
おやすみぃ~ ∧∧ [(^-^*)] ┌∪─∪┐ │◆◇◆│ │◇◆◇│ └───┘ http://emoji.vis.ne.jp/oyasumi36.htm[/code]
相关文章推荐
- Android Native 绘图方法
- 红宝石(Ruby)史话
- Ruby简明教程之循环语句介绍
- ruby 一些简单的例子
- Ruby on Rails在Ping ++ 平台实现支付
- Ruby 之 class 中的 private、 protected、public
- Ruby中操作文件的方法介绍
- 详解Ruby中的单件方法和单件类
- Ruby中的集合编写指南
- CentOS 6.3下编译安装Ruby 2.0笔记
- Ruby基本的环境变量设置以及常用解释器命令介绍
- 详解Ruby中的代码块及其参数传递
- Ruby编程中关于中断和返回的用法教程
- Ruby中的异常处理代码编写示例
- Ruby程序中发送基于HTTP协议的请求的简单示例
- Ruby中的变量学习总结
- 几个加速Ruby on Rails的编程技巧
- Ruby配置rspec和RestClient来检测服务器
- 分析Cache 在 Ruby China 里面的应用情况