[Groovy]クラスでthisキーワードを使用する

書式
this.メンバー変数 =値

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Arkgame {
//変数の宣言
int age =456;
//メソッドを定義
public int getAge() {
//thisキーワードを使用
this.age =123
return age;
}
static void main(String[] args) {
//インスタンスagを生成
Arkgame ag = new Arkgame();
//getAgeメソッドを呼び出す
println(ag.getAge());
}
}
class Arkgame { //変数の宣言 int age =456; //メソッドを定義 public int getAge() { //thisキーワードを使用 this.age =123 return age; } static void main(String[] args) { //インスタンスagを生成 Arkgame ag = new Arkgame(); //getAgeメソッドを呼び出す println(ag.getAge()); } }
class Arkgame { 
  //変数の宣言
   int age =456; 
   
   //メソッドを定義
   public int getAge() { 
    //thisキーワードを使用
    this.age =123
    return age;
   } 
      
   static void main(String[] args) { 
      //インスタンスagを生成
      Arkgame ag = new Arkgame();
      //getAgeメソッドを呼び出す
      println(ag.getAge()); 
   }  
}

結果
123

Groovy

Posted by arkgame