初入江湖
查看详细资料
TOP
江湖新秀
社区管理员
原帖由 neohsiao 于 2008-6-16 10:54 发表 self表示调用当前的类 那么a=class A puts "A" def WoW puts "WoW" end self.WoW end这样为什么不行?是不是方法一定要实例化才能调用?或者说怎样在类里面就调用类里面的方法?
原帖由 cjq_999 于 2008-6-26 20:14 发表 我认为是先把A这个空对象赋值给a,然后A被定义了,而a还是空的 如果想a不是为空,需要在外面赋值:a=A class Object puts "A" def WoW puts "WoW" end end a=A 这个时候a就等于A了
1 #!/usr/bin/ruby 2 #puts "hello,Ruby" 3 #def foo 4 # puts "hello,world" 5 #end 6 class A 7 def self.WoW 8 puts "WoW" 9 end 10 self.WoW 11 end
原帖由 liuxueming 于 2008-6-30 17:41 发表 我还是不太懂这段代码有什麽用,仅仅是为了执行 class A中的puts 语句吗?对a来说没 有什么用啊。
a = class A 7 def self.WoW 8 puts "WoW" 9 end 10 self.WoW 11 end 12 13 puts a.class
a = class A 7 def self.WoW 8 puts "WoW" 9 end 10 self.WoW 11 end 12 13 puts a.class.class
1 #!/usr/bin/ruby 2 3 #重载Class类 4 class Class 5 alias oldNew new 6 def new(*args) 7 print "Creating a new ", self.name, "\n" 8 oldNew(*args) 9 end 10 11 end 12 13 #定义1个新的类(这里仅仅是声明,不会产生输入) 14 15 class A 16 def WoW 17 puts 'wow' 18 end 19 end 20 21 #实例化A类10次,每次实例化A类,都会产生一个nilclass实例,nilclass是class类的一个实例,所以产生了10次输出. 22 for i in (1..10) 23 A.new 24 end