考虑下面的代码
class Repository{
public static Item i; //Item is a type (class)
GetItem(){
// initialize i if null. Read i from an xml file if the last write time of file is greater than last read time else return current i
return i;
}
SaveItem(item){
//save i;
//write i to xml file
i=item;
}
}
class User{
public static void Main(){
Repository r = new Repository();
r.GetItem().MakeChangesToItem(); //method inside item to make some changes
r.SaveItem(r.GetItem());
}
}
这段代码是否有可能偶尔出现。显然它对我有用。有时更改会反映在静态项目中,有时不会。当我将 Main 方法代码更改为
Item i=new Repository().GetItem();
i.MakeChangesToItem();
r.SaveItem(i);
它工作正常。有没有人经历过这种情况?谢谢