ruby类问题
我有以下ruby代码:ruby类问题,ruby,Ruby,我有以下ruby代码: class Mp def initialize Test.new.mytest Work.new.mywork ha address end def ha puts "message from ha" end def address a='see' end end class Test def mytest m=Mp.new puts "Message from
class Mp
def initialize
Test.new.mytest
Work.new.mywork
ha
address
end
def ha
puts "message from ha"
end
def address
a='see'
end
end
class Test
def mytest
m=Mp.new
puts "Message from test do you #{m.address}"
end
end
class Work
def mywork
puts "message from work"
end
end
Mp.new
除了def mytest中我试图输出m地址的部分之外,这一切都很好。提前感谢您的帮助。您有一个无限循环。你创建了一个新的class
Mp
对象,它又创建了一个classTest
对象,然后调用它的mytest
方法,它又创建了classMp
的另一个对象,这反过来…你得到了一个无限循环。您创建了一个新的classMp
对象,它又创建了一个classTest
对象,然后调用它的mytest
方法,它又创建了另一个classMp
对象,这反过来又…实际上它不工作的原因与打印地址无关。在这之前有一行:
m=Mp.new
这将创建一个新的Mp
对象。但是在Mp
的初始化方法中,会创建一个新的Test
对象,并调用其mytest
方法。然后,mytest
方法再次创建一个新的Mp对象,依此类推。换句话说:Test#mytest
和Mp#initialize
是相互且无限递归的
根据您的评论进行编辑:
我不太确定我是否理解这个问题。如果您的意思是“在调用address
后,如何访问address
方法中设置的变量a
”,则您不会这样做a
是一个局部变量,一旦方法返回,它就超出范围。如果要设置实例变量,请使用@a='请参阅'
@
表示ruby中的实例变量。如果希望能够从对象外部访问该变量,请使用attr\u accessor:a
为@a
定义访问器方法
例如:
class Mp
attr_accessor :c
def initialize
initialize_variables
puts @c
puts @b # I can access @c and @b here because it's an instance variable
# and I'm within the same object
# puts a # This does not work because a is a local variable from the
# initialize_variables method and no longer in scope
end
def initialize_variables
a = "a"
@b = "b"
@c = "c"
puts a # I can access a here because I'm still inside the method
# where a was defined
end
end
m = Mp.new
# puts m.a
# puts m.b # These don't work because there are no methods a or b
puts m.c # This works because attr_accessor defined a method c which
# returns the content of m's @c variable
实际上,它不起作用的原因与打印地址无关。在这之前有一行:
m=Mp.new
这将创建一个新的Mp
对象。但是在Mp
的初始化方法中,会创建一个新的Test
对象,并调用其mytest
方法。然后,mytest
方法再次创建一个新的Mp对象,依此类推。换句话说:Test#mytest
和Mp#initialize
是相互且无限递归的
根据您的评论进行编辑:
我不太确定我是否理解这个问题。如果您的意思是“在调用address
后,如何访问address
方法中设置的变量a
”,则您不会这样做a
是一个局部变量,一旦方法返回,它就超出范围。如果要设置实例变量,请使用@a='请参阅'
@
表示ruby中的实例变量。如果希望能够从对象外部访问该变量,请使用attr\u accessor:a
为@a
定义访问器方法
例如:
class Mp
attr_accessor :c
def initialize
initialize_variables
puts @c
puts @b # I can access @c and @b here because it's an instance variable
# and I'm within the same object
# puts a # This does not work because a is a local variable from the
# initialize_variables method and no longer in scope
end
def initialize_variables
a = "a"
@b = "b"
@c = "c"
puts a # I can access a here because I'm still inside the method
# where a was defined
end
end
m = Mp.new
# puts m.a
# puts m.b # These don't work because there are no methods a or b
puts m.c # This works because attr_accessor defined a method c which
# returns the content of m's @c variable
我明白了,那么我如何访问def address中的字符串a='see'?因此,在我的Mp类中,如果我有def address并且它有一个='see',我可以使用@a访问测试中的a?你能举个简单的例子吗?Thanks@MAP:否。如果执行
a='see'
,则在方法返回后,您根本无法访问a
。如果您执行了@a='see'
,则可以访问@a
,但只能在同一对象内访问,而不能从外部访问。如果在方法中执行@a='see'
,在类定义中执行attr\u accessor:a
,则可以通过执行my\u mp\u object.a
从外部访问@a
。我将发布一个例子。谢谢你的帮助和耐心!我明白了,那么我如何访问def address中的字符串a='see'?因此,在我的Mp类中,如果我有def address并且它有一个='see',我可以使用@a访问测试中的a?你能举个简单的例子吗?Thanks@MAP:否。如果执行a='see'
,则在方法返回后,您根本无法访问a
。如果您执行了@a='see'
,则可以访问@a
,但只能在同一对象内访问,而不能从外部访问。如果在方法中执行@a='see'
,在类定义中执行attr\u accessor:a
,则可以通过执行my\u mp\u object.a
从外部访问@a
。我将发布一个例子。谢谢你的帮助和耐心!