[Groovy]クラスでメソッドを作る方法

書式
クラス名 インスタンス名 = new クラス名();
インスタンス名.メソット名(引数)
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//クラスArkgameの定義
class Arkgame {
//変数の宣言
int age;
// getメソッドを定義
public int getAge() {
return age;
}
//setメソッドを定義
public void setAge(int n) {
age = n;
}
//Mainプログラム
static void main(String[] args) {
//クラスのインスタンスを生成
Arkgame ag = new Arkgame();
//setメソッドを使用
ag.setAge(34);
//getメソッドを呼び出す
println(ag.getAge());
}
}
//クラスArkgameの定義 class Arkgame { //変数の宣言 int age; // getメソッドを定義 public int getAge() { return age; } //setメソッドを定義 public void setAge(int n) { age = n; } //Mainプログラム static void main(String[] args) { //クラスのインスタンスを生成 Arkgame ag = new Arkgame(); //setメソッドを使用 ag.setAge(34); //getメソッドを呼び出す println(ag.getAge()); } }
//クラスArkgameの定義
class Arkgame { 
   //変数の宣言
   int age; 
   // getメソッドを定義
   public int getAge() { 
      return age; 
   } 
   //setメソッドを定義
   public void setAge(int n) { 
      age = n; 
   } 
  //Mainプログラム
   static void main(String[] args) { 
      //クラスのインスタンスを生成
      Arkgame ag = new Arkgame(); 
      //setメソッドを使用
      ag.setAge(34); 
      //getメソッドを呼び出す
      println(ag.getAge()); 
   } 
}

実行結果
34

Groovy

Posted by arkgame