當你說的安全,你的意思是避免意外的代碼修改? 結構化的方式可以像下面這樣。 如果你想使它真的'安全',那麼你可以修改代碼來在調用類中存儲一個字符串* HashCode *,並且只有在Egg中匹配(內部調用)時,才允許修改。
Interface ISecureModifier
{
String GetSecureModifierKEY();
String GetSecureModifierVALUE();
}
class Egg
{
Dictionary Secure_ata;
public secureDataModifier(ISecureModifier modifyingObject)//note the interface being used
{
//Here, try a cast (if your compiler still allowed other type objects not implementing ISecureModifier ) and throw exception stating not authorized to modify.
modifyingObject.GetSecureModifierKEY
modifyingObject.GetSecureModifierValue
/*Now write the code to modify Dictionary*/
}
}
class EggSac:ISecureModifier//implements interface
{
private string SecureModifierKEY;
private string SecureModifierVALUE
String GetSecureModifierKEY()//inteface impl
{
return SecureModifierKEY;
}
String GetSecureModifierVALUE();//interface impl
{
return SecureModifierVALUE;
}
ModifySecureData(Egg egg, string key, string value)
{
egg.secureDataModifier(this);//passing own reference
}
}
你可以叫這樣
objEggSack.ModifySecureData(objEgg101, "firstKey","NewValue")
神奇。乾杯。 – Slobaloba 2011-06-05 13:01:48