類的實現內部,可以再包含類的定義,稱之爲嵌套類
備註:訪問嵌套類的全局變量,需要嵌套類的全路徑,見下面的例子。
class animal
func __init__(self, type)
self.type = type
end
func __del__(self)
end
class bird
func __init__(self, name, age)
self.name = name
self.age = age
end
func __del__(self)
end
class bigbird
sum = 0
func __init__(self, big)
self.big = big
animal.bird.bigbird.sum = animal.bird.bigbird.sum + 1
//下面的調用方式錯誤,無法找到這個類,哪怕是在類的函數的內部,需要用上面的方式,用類的完整路徑
//bigbird.sum = bigbird.sum + 1
end
func __del__(self)
animal.bird.bigbird.sum = animal.bird.bigbird.sum - 1
end
endclass
endclass
endclass
測試
_animal = animal("鳥類")
_bird = _animal.bird("天鵝", 5)
_bigbird = _bird.bigbird(123)
_bigbird2 = animal.bird.bigbird(456)
js.debug.log("log 共" + animal.bird.bigbird.sum + "只鳥" + "\t 第一隻鳥多大?" + _bigbird.big + "\t 第二隻鳥多大?" + _bigbird2.big)
運行結果:
[26376] log 共2只鳥 第一隻鳥多大?123 第二隻鳥多大?456