采取以下代码:
private var m_iQuanitity:int;
public function get quantity():int
{
return m_iQuantity;
}
这似乎完全有道理。您可以毫无问题地从外部课程中看到数量是多少,但您根本无法真正弄乱它。现在采取以下代码:
private var m_acUsers:ArrayCollection = new ArrayCollection();
public function get users():ArrayCollection
{
return m_acUsers;
}
在这种情况下,您不能真正直接设置变量,但您仍然可以对它进行几乎所有其他事情,而不会出现任何问题。您可以调用它的 AddItem 和 RemoveItemAt 函数,它们可以做很多事情来“设置”变量。
Does it still make sense to do this? I know you can create a duplicate ArrayCollection and just pass the duplicate back to avoid allowing it to be set, but doing stuff like that all over the place, purely for defensive programming, can waste a lot of CPU time. So I guess I'm asking if it still makes sense anyway, how so, and if I'm missing the point of using get and set completely? Thanks!