【发布时间】:2010-09-30 01:35:54
【问题描述】:
我可以创建一个可以被类方法调用的私有实例方法吗?
class Foo
def initialize(n)
@n = n
end
private # or protected?
def plus(n)
@n += n
end
end
class Foo
def Foo.bar(my_instance, n)
my_instance.plus(n)
end
end
a = Foo.new(5)
a.plus(3) # This should not be allowed, but
Foo.bar(a, 3) # I want to allow this
抱歉,如果这是一个非常基本的问题,但我无法通过 Google 找到解决方案。
【问题讨论】:
-
你应该解决你的问题,你有一个错字。方法是 bar 还是 plus?
-
你是对的 - 将修复。谢谢。
标签: ruby oop access-specifier