GROOVY 函数

class Example {
   static int x = 100;

   public int getX(){
      this.x = 200;
      return x;
   }

   public int setX(int pX){
      x = pX;
   }
    
   static void main(String[] args) {
      Example ex = new Example(); 
      println(ex.getX());
   } 
}
class Example { 
   static void sum(int a,int b){
      int c = a+b
      println(c)
   }

   static void main(String[] args) { 
      sum(10,20)
   }
}
class Example { 
   static def DisplayName(){
      println("this is how methods work in groovy")
      println("this is an example of a simple method")
   }

   static void main(String[] args) { 
      // x is defined as a variable 
      DisplayName()
   }
}
class Example {
   static int sum(int a,int b = 5) {
      int c = a+b;
      return c;
   } 
	
   static void main(String[] args) {
      println(sum(6));
   } 
}
class Example { 
   static void sum(int a,int b = 10){
      int c = a+b
      println(c)
   }

   static void main(String[] args) { 
      sum(10)
   }
}

 

posted @ 2025-07-07 10:26  山村放羊娃  阅读(13)  评论(0)    收藏  举报