共計 456 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 Java 中,可以通過 this 關鍵字來調用當前對象的方法。this 關鍵字代表當前對象的引用,可以在類的方法中使用它來調用當前對象的其他方法。例如:
public class MyClass {public void method1() {System.out.println("method1 is called");
}
public void method2() {System.out.println("method2 is called");
// 調用當前對象的 method1 方法
this.method1();}
public static void main(String[] args) {MyClass myObject = new MyClass();
myObject.method2();}
}
在上面的例子中,method2 方法中使用 this 關鍵字調用了當前對象的 method1 方法。當 main 方法中調用 myObject.method2() 時,輸出結果為:
method2 is called
method1 is called
丸趣 TV 網 – 提供最優質的資源集合!
正文完