请教Ruby的一个问题 新手
ruby吧
全部回复
仅看楼主
level 1
bdgboss 楼主
本人按照书本编程,发现个问题
方法:
Class MyClass
def attr1
@attr1
end
end
myclass = MyClass.new
puts my_class.attr1
请问最后一行的my_class.attr1中调用的attr1到底是函数还是局部变量
完整代码是:
Class MyClass
def attr1
@attr1
end
def attr2=(value)
@attr2 = value
end
end
myclass = MyClass.new
my_class.attr1 = "attr1"
puts my_class.attr1
my_class.attr2 = "attr2"
puts my_class.attr2
程序运行到最后一行出错了,提示说什么“未定义方法attr2=” 晕倒
请大虾给予指正 谢谢

2012年10月06日 02点10分 1
level 9
建议先看入门教程。
2012年10月06日 08点10分 3
level 10
attr2属性是封装在类里的,除非:
def attr2
@attr2
end
2012年11月14日 13点11分 4
level 9
方法名后面跟个等于号,这是应该避免的用法。
http://blog.sina.com.cn/s/blog_7833c8450100solx.html
2012年11月15日 00点11分 5
level 1
ruby方法调用如果不传参数可以不用加括号。懂了吧
2013年04月07日 16点04分 6
1