如何延迟从 Java 中的方法返回变量,或者如果不希望这样做,我应该怎么做?
考虑一下:
public class Transaction {
public int addInsert() {
...
return insertId;
}
public boolean addUpdate() {
...
return updateSuccesful;
}
public void commit() {
/* Calls everything that is inserted via addInsert or addUpdate. */
}
}
现在假设您将代码用作:
Transaction transaction = new Transaction();
int insertedId = transaction.addInsert();
boolean updateSuccesful = transaction.addUpdate();
//insertId, updateSuccesful cannot be known yet
transaction.commit();
//now insertId, updateSuccesful should be filled in
所以返回可能只有transaction.commit()
在被调用时才会发生。
有什么想法吗?