public abstract class BaseActivity extends FragmentActivity implements UiOperation {
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
Utils.findButtonSetOnClickListener(rootView, **this**); // 此處 this 是什麼?
}
}
interface UiOperation extends View.OnClickListener {}
class Utils {
static void findButtonSetOnClickListener(View view, View.OnClickListener listener) { ... }
}
this 代表 **當前 `BaseActivity` 子類實例的引用**(即 `this` 指向調用 `onCreate` 的那個 Activity 對象)。
| 場景 | 示例 | 含義 |
|---|---|---|
| 區分同名字段 | `this.name = name;` | 左邊是實例字段,右邊是參數 |
| 構造函數鏈調用 | `this(name, age);` | 調用本類另一構造器 |
| 傳遞當前對象 | `other.doSomething(this);` | 作為參數傳給他方 |
| 返回當前對象 | `return this;` | 鏈式調用/Builder 模式 |