Monkey/語言/對映
外觀
對映是需要鍵值關係的列表。
Monkey 中的對映透過擴充套件 Map 類來建立。擴充套件的對映類 *必須* 實現 Compare() 方法。
對映能夠使用任何型別,包括物件。
Monkey 中的 Compare() 方法在值相等時返回 “0”,在值更高時返回 “1” 或更大,在值更低時返回 “-1” 或更小。一些物件難以比較,但該方法必須為鍵中所需的每個欄位返回一個值。
Class VectorMap <Vector> Extends Map<Vector, Vector>
Method Compare:Int( lhs:Vector,rhs:Vector )
If lhs.x<rhs.x Return -1
If lhs.x>rhs.x Return 1
If lhs.y<rhs.y Return -1
If lhs.y>rhs.y Return 1
If lhs.z<rhs.z Return -1
Return lhs.z>rhs.z
End
End
Local mymap:VectorMap<Vector> = New VectorMap<Vector>
對映使用 Get() 和 Set() 方法。
Local somevector:Vector = New Vector(1,2,3)
Local somevalue:Vector = New Vector(4,5,6)
mymap.Set( somevector, somevalue)
Local returnvalue:Vector = mymap.Get(somevector)
Monkey 包含內建對映,它們提供用於設定物件的功能,方法是使用整數、浮點數和字串作為鍵:IntMap、FloatMap、StringMap