Groovy中的方法重写

时间:2015-02-27 13:09:42

标签: inheritance groovy

如何在Groovy中覆盖扩展超类的类中超类的方法? Java方法不起作用,因为两个方法(超类中的一个和子类中的方法)正在执行。例如:

class SuperClass {

  SuperClass(){

    println("This is the superclass")
  }

  def awaitServer(){
    println("awaiting server in the superclass")
  }

}


class SubClass extends SuperClass{


  SubClass(){
    println("This is the subclass")
  }

  @Override
  def awaitServer(){
    println("awaiting server in the subclass")
  }

}

//////
SubClass sb = new SubClass()
sb.awaitServer()

我得到的输出是:

awaiting server in the superclass
awaiting server in the subclass

正如你所看到的那样,当我在子类中覆盖超类的方法时,会执行这两种方法。为什么会这样?如何在Groovy中完成方法覆盖?

我在网上搜索过,但我无法弄明白。有人可以提供样本或简单的例子吗?

提前谢谢你,

1 个答案:

答案 0 :(得分:3)

您忘了添加def。此代码可以正常工作:

class SuperClass {
   SuperClass(){
      println("This is the superclass")
   }

   def awaitServer() {
      println("awaiting server in the superclass")
   }
}

class SubClass extends SuperClass{

   SubClass() {
      println("This is the subclass")
   }

   @Override
   def awaitServer() {
      println("awaiting server in the subclass")
   }
}

SubClass sb = new SubClass()
sb.awaitServer()

输出:

This is the superclass 
This is the subclass 
awaiting server in the subclass

看看下面的输出:

[opal@opal-mac-2]/tmp % cat lol.groovy 
class SuperClass {
   SuperClass(){
      println("This is the superclass")
   }

   def awaitServer() {
      println("awaiting server in the superclass")
   }
}

class SubClass extends SuperClass{

   SubClass() {
      println("This is the subclass")
   }

   @Override
   def awaitServer() {
      println("awaiting server in the subclass")
   }
}

SubClass sb = new SubClass()
sb.awaitServer()

[opal@opal-mac-2]/tmp % groovy -v
Groovy Version: 2.4.0 JVM: 1.8.0_05 Vendor: Oracle Corporation OS: Mac OS X
[opal@opal-mac-2]/tmp % groovy lol.groovy 
This is the superclass
This is the subclass
awaiting server in the subclass

使用groovy 1.8.6:

[opal@opal-mac-2]/tmp % gvm use groovy 1.8.6
==== BROADCAST =================================================================
* 27/02/15: Springboot 1.1.11.RELEASE has been released on GVM. #spring
* 27/02/15: Springboot 1.2.2.RELEASE has been released on GVM. #spring
* 26/02/15: Grails 3.0.0.M2 has been released on GVM. #grailsframework
================================================================================

Stop! groovy 1.8.6 is not installed.
Do you want to install it now? (Y/n): Y

Downloading: groovy 1.8.6

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 15.5M  100 15.5M    0     0  2666k      0  0:00:05  0:00:05 --:--:-- 3716k

Installing: groovy 1.8.6
/Users/opal/.gvm/tmp/groovy-1.8.6 -> /Users/opal/.gvm/groovy/1.8.6
Done installing!


Using groovy version 1.8.6 in this shell.
[opal@opal-mac-2]/tmp % groovy lol.groovy 
This is the superclass
This is the subclass
awaiting server in the subclass