【发布时间】:2019-07-03 15:20:13
【问题描述】:
我想为类中的内部类创建对象。所有内部类方法都使用一个公共变量,需要在创建外部类时对其进行初始化。
class A
@x = nil
@y = nil
def initialize(x, y)
@x = x
@y = y
end
class B
def func_b
puts "x- #{@x} and y- #{@y} in class B"
end
end
class C
def func_c
puts "x- #{@x} and y- #{@y} in class C"
end
end
end
我能够为 B 和 C 之类的类创建对象
#1
b = A::B.new
#2
a = A
c = a::C.new
但我想在为B 或C 创建对象之前初始化A
类似
a = A.new(4,5)
b = a::C.new
但这似乎不起作用。我该怎么做。
【问题讨论】:
标签: ruby class constructor inner-classes