2007-12-21
类中的成员变量可以这样声明, 但访问不能用@
关键字: 语法
大家先看下面代码:
输出是:
say age: 30
hello age: 30
say age: 20
hello age: 20
如果使用:
那么什么都不输出,应为@age 为nil
如果使用:
那么输出是:
say age: age
hello age: age
say age: age
hello age: age
:age 符号而已
ActiveRecord的低层有使用这种方式申明class
文件:schema_definitions.rb
路径:%RUBY_HOME%\\lib\ruby\gems\1.8\gems\activerecord-2.0.1\lib\active_record\connection_adapters\abstract
class Person < Struct.new(:age)
def say_age
puts "say age: #{age}" if age != nil
end
def hello_age
puts "hello age: #{age}" if age!=nil
end
end
p = Person.new(30)
p.say_age
p.hello_age
p.age = 20
p.say_age
p.hello_age
输出是:
say age: 30
hello age: 30
say age: 20
hello age: 20
如果使用:
def say_age
puts "say age: #{@age}" if @age != nil
end
def hello_age
puts "hello age: #{@age}" if @age!=nil
end
那么什么都不输出,应为@age 为nil
如果使用:
def say_age
puts "say age: #{:age}" if :age != nil
end
def hello_age
puts "hello age: #{:age}" if :age!=nil
end
那么输出是:
say age: age
hello age: age
say age: age
hello age: age
:age 符号而已
ActiveRecord的低层有使用这种方式申明class
class ColumnDefinition < Struct.new(:base, :name, :type, :limit, :precision, :scale, :default, :null) #:nodoc: .. .. .. end
文件:schema_definitions.rb
路径:%RUBY_HOME%\\lib\ruby\gems\1.8\gems\activerecord-2.0.1\lib\active_record\connection_adapters\abstract
评论
lemonzc
2007-12-21
例子:
感觉就是最纯洁的 Java Bean了。 哈哈。。。。。Ruby Stone
# Create a structure with a name in Struct
Struct.new("Customer", :name, :address) #=> Struct::Customer
Struct::Customer.new("Dave", "123 Main") #=> #<Struct::Customer name="Dave", address="123 Main">
# Create a structure named by its constant
Customer = Struct.new(:name, :address) #=> Customer
Customer.new("Dave", "123 Main") #=> #<Customer name="Dave", address="123 Main">
感觉就是最纯洁的 Java Bean了。 哈哈。。。。。Ruby Stone
lemonzc
2007-12-21
哎哦哦。。。查了文档才知道自己自作多情了。晕
A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.
The Struct class is a generator of specific classes, each one of which is defined to hold a set of variables and their accessors. In these examples, we‘ll call the generated class ``CustomerClass,’’ and we‘ll show an example instance of that class as ``CustomerInst.’‘
In the descriptions that follow, the parameter symbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).
A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.
The Struct class is a generator of specific classes, each one of which is defined to hold a set of variables and their accessors. In these examples, we‘ll call the generated class ``CustomerClass,’’ and we‘ll show an example instance of that class as ``CustomerInst.’‘
In the descriptions that follow, the parameter symbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 882 次
- 性别:

- 来自: 成都

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Ruby 在 Notepad++ 上飞 ...
所以说Notepad++对于处理SJIS等非Unicode编码的文件还是无解咯? ...
-- by justps -
Ruby 在 Notepad++ 上飞 ...
notepad++ 功能还不错, 但性能差强人意, 打开超过 3000 的文本基 ...
-- by Feiing -
Ruby 在 Notepad++ 上飞 ...
kenlistian 写道我感觉editplus也是不错啊,装了个ultraed ...
-- by qubic -
Ruby 在 Notepad++ 上飞 ...
我感觉editplus也是不错啊,装了个ultraedit和editplus,觉 ...
-- by kenlistian -
Ruby 在 Notepad++ 上飞 ...
femto 写道能支持函数列表么?就是类似于其他IDE里头,在某个文件时, 左边 ...
-- by flynetcn






评论排行榜