第三十五章 Objects - 集合类

2023-09-22  本文已影响0人  Cache技术分享

第三十五章 Objects - 集合类

集合类

当需要一个容器来存储相关值集时,可以使用 $LIST 格式列表和多维数组。

如果更喜欢使用类,系统 提供列表类和数组类;这些称为集合。

用作独立对象的列表和数组类

要创建列表对象,可以使用以下类:

列表中的元素按顺序排列。它们在列表中的位置可以使用范围从 1N 的整数键来访问,其中 N 是最后一个元素的位置。

要操作列表对象,请使用其方法。例如:

 set Colors = ##class(%Library.ListOfDataTypes).%New()
 do Colors.Insert("Red")
 do Colors.Insert("Green")
 do Colors.Insert("Blue")

 write "Number of list items: ", Colors.Count()
 write !, "Second list item: ", Colors.GetAt(2)

 do Colors.SetAt("Yellow",2)
 write !, "New second item: ", Colors.GetAt(2)

 write !, "Third item before insertion: ", Colors.GetAt(3)
 do Colors.InsertAt("Purple",3)
 write !, "Number of items after insertion: ", Colors.Count()
 write !, "Third item after insertion: ", Colors.GetAt(3)
 write !, "Fourth item after insertion: ", Colors.GetAt(4)

 do Colors.RemoveAt(3)
 write "Number of items after removing item 3: ", Colors.Count()

 write "List items:"
 for i = 1:1:Colors.Count() write Colors.GetAt(i),!
import iris

Colors=iris.cls("%Library.ListOfDataTypes")._New()
Colors.Insert("Red")
Colors.Insert("Green")
Colors.Insert("Blue")

print("Number of list items:", Colors.Count())
print("Second list item:", Colors.GetAt(2))

Colors.SetAt("Yellow",2)
print("New second item: ", Colors.GetAt(2))

print("Third item before insertion: ", Colors.GetAt(3))
Colors.InsertAt("Purple",3)
print("Number of items after insertion: ", Colors.Count())
print("Third item after insertion: ", Colors.GetAt(3))
print("Fourth item after insertion: ", Colors.GetAt(4))

Colors.RemoveAt(3)
print("Number of items after removing item 3: ", Colors.Count())

print("List items:")
for i in range(1, Colors.Count() + 1) print(Colors.GetAt(i))

同样,要创建数组对象,可以使用以下类:

要操作数组对象,请使用其方法。例如:

 set ItemArray = ##class(%Library.ArrayOfDataTypes).%New()
 do ItemArray.SetAt("example item","alpha")
 do ItemArray.SetAt("another item","beta")
 do ItemArray.SetAt("yet another item","gamma")
 do ItemArray.SetAt("still another item","omega")
 write "Number of items in this array: ", ItemArray.Count()
 write !, "Item that has the key gamma: ", ItemArray.GetAt("gamma")
 set ItemArray = ##class(%Library.ArrayOfDataTypes).%New()
 do ItemArray.SetAt("example item","alpha")
 do ItemArray.SetAt("another item","beta")
 do ItemArray.SetAt("yet another item","gamma")
 do ItemArray.SetAt("still another item","omega")
 write "Number of items in this array: ", ItemArray.Count()
 write !, "Item that has the key gamma: ", ItemArray.GetAt("gamma")

SetAt() 方法将项目添加到数组中,其中第一个参数是要添加的元素,第二个参数是键。数组元素按键排序,数字键在前,从小到大排序,字符串键在后,按字母顺序排序,大写字母在前,小写字母在前。例如:-2-1012AAAABaaaab

列表和数组作为属性

还可以将属性定义为列表或数组。

要将属性定义为列表,请使用以下形式:

Property MyProperty as list of Classname;

如果 Classname 是数据类型类,则 IRIS 使用 %Collection.ListOfDT提供的接口。如果 Classname 是对象类,则它使用 %Collection.ListOfObj 提供的接口。

要将属性定义为数组,请使用以下形式:

Property MyProperty as array of Classname;

如果 Classname 是数据类型类,则 IRIS 使用 %Collection.ArrayOfDT 提供的接口。如果 Classname 是对象类,则它使用 %Collection.ArrayOfObj提供的接口。

上一篇下一篇

猜你喜欢

热点阅读