下面是超类/子类构造的示例:
C:\>irb --simple-prompt
>> class Parent
>> @@x = 10
>> end
=> 10
>> class Child < Parent
>> @@x = 12
>> end
=> 12
>> class Parent
>> puts "@@X = #{@@x}"
>> end
@@X = 12
=> nil
并且上面也理解了。但是我想检查当两个类被单独定义为独立类时是否可能来定义它们之间的超级/子关系?
我尝试了以下方法,但它不起作用。可能不是我尝试的方式:
C:\>irb --simple-prompt
>> class Parent
>> @@X = 10
>> end
=> 10
>> class Child
>> @@x = 15
>> end
=> 15
>> class Child < Parent
>> def show
>> p "hi"
>> end
>> end
TypeError: superclass mismatch for class Child
from (irb):7
from C:/Ruby193/bin/irb:12:in `<main>'
>>