2015-02-27 4 views
2

Как переопределить метод из суперкласса в классе, который расширяет суперкласс, в Groovy? Путь Java не работает, поскольку выполняются оба метода (один в суперклассе и один в подклассе). Например:Переопределение метода в 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() 

вывода я получаю:

awaiting server in the superclass 
awaiting server in the subclass 

Как вы можете видеть оба метода выполняются, когда я буду перекрывая метод суперкласса в подклассе. Почему это происходит? Как метод переопределяется в Groovy?

Я искал в Интернете, но им не удалось это выяснить. Может ли кто-нибудь предоставить образец или простой пример?

Спасибо заранее,

+1

I думаю, что вы потеряли что-то в своем переводе на пример, как работает ... –

+0

он будет печатать один из двух, а не оба - независимо от того, что происходит. если вы не забыли в своем примере, что в подклассе в 'awaitServer' есть вызов' super'. – cfrick

ответ

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 

Посмотрите ниже выхода:

[[email protected]]/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() 

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

С заводной 1.8.6:

[[email protected]]/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. 
[[email protected]]/tmp % groovy lol.groovy 
This is the superclass 
This is the subclass 
awaiting server in the subclass 
+0

Hi Opal Да, я забыл написать «def» в этом посте, но в моем коде у меня есть def перед именем метода. Но все же оба метода выполняются. – Pier

+0

Какая отличная версия вы используете? – Opal

+0

Я использую ** Groovy: 1.8.6 ** – Pier

Смежные вопросы