开发者

Error: What's wrong with this Groovy code?

What's wrong with this Groovy code?

class Pretending {
    public int count=0
    Object get(String name)
    {
        return 'hi'
    }
    void set(String name, Object value)
    {
        count++;
    }
    def anto = new Pretending();

Here I'm trying to extend the functionality of Groovy set/get method, but when I try to execute this script by groovy Pret (where my file name is Pret), I'm getting an error on the last line stating unexpected token. What's the reason for this? I'm a newbie to the Groovy world, so I wasn't able to figure 开发者_运维技巧it out!


class Pretending {
  public int count=55
  Object get(String name) {
    'hi'
  }
  void set(String name,Object value) {
    count++;
  }
}
a = new Pretending()

assert a.any_undef_var == 'hi'
assert a.count == 55
assert a.@count == 55
a.set "v", 4
assert a.count == 56

i think this is what you are trying to achieve + extras


You are missing a curly brace. Try:

 class Pretending {
   public int count=0
   Object get(String name)
   {
      return 'hi'
   }
   void set(String name,Object value)
   {
     count++;
   }
   def anto = new Pretending();
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜