我在Android代碼(以及其他一些代碼源)中看到了[下文]。它的目的或目的是什麼?方法中變量複製的目的是什麼?
class Foo {
int mBar = 1337;
static void main(String... args) {
System.out.println(isFubar());
}
boolean isFubar() {
int ret = mBar; // <--- Focus of attention
if (ret == 666)
return true;
else
return false;
}
}
這似乎是浪費時間和資源。 mBar顯然沒有被修改。它沒有被修改的風險(在給定的上下文中),那麼爲什麼要複製這個圖元只是爲了對它進行非侵入檢查並返回呢?在Android源
public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF dragRect) {
final boolean portrait = mPortrait; <--- Here it is
final int cellWidth = mCellWidth;
final int cellHeight = mCellHeight;
final int widthGap = mWidthGap;
final int heightGap = mHeightGap;
final int hStartPadding = portrait ? mShortAxisStartPadding : mLongAxisStartPadding;
final int vStartPadding = portrait ? mLongAxisStartPadding : mShortAxisStartPadding;
int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap);
int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap);
int x = hStartPadding + cellX * (cellWidth + widthGap);
int y = vStartPadding + cellY * (cellHeight + heightGap);
dragRect.set(x, y, x + width, y + height);
}
你能指出具體的例子嗎? –
我不知道,但我會看看歷史,看看是否有可能是一個重要的版本。 –
也許開發人員只是討厭「m疣」。但是,通過製作副本並使副本最終確認,他/她表示該方法不會修改該字段。 –