简体   繁体   English

在Groovy类中实现Java接口

[英]Implement Java Interface in a Groovy class

I have just started the programming in Groovy. 我刚刚开始使用Groovy进行编程。 I noticed one strange behavior and unable to find any explanation for the same. 我注意到一个奇怪的行为,无法找到相同的解释。

I have created a Java Interface TestInterface.java 我创建了一个Java接口TestInterface.java

public interface TestInterface {

    public void m1();

}

I have created a Groovy class TestG.groovy 我创建了一个Groovy类TestG.groovy

class TestG implements TestInterface {

}

I have created a Java class TestJ.java 我创建了一个Java类TestJ.java

public class TestJ implements TestInterface{

    @Override
    public void m1() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    } 
}

My problem is in TestG why I do not get any error to implement abstract method or declare class as abstract. 我的问题在于TestG为什么我没有得到任何错误来实现抽象方法或将类声明为抽象。

What does differ in java and groovy as I needed to implement abstract methods or declare class as abstract in Java but not in Groovy. java和groovy有什么不同,因为我需要实现抽象方法或在Java中将类声明为抽象而不是在Groovy中。

I know this question has been out for a while and answered above but I felt the need to add this. 我知道这个问题已经有一段时间了,并在上面回答,但我觉得有必要加上这个。

class TestG implements TestInterface {}

this is still the "java" way of doing things. 这仍然是“java”做事方式。 Using groovy essentially eliminates the need of implementing interfaces (except just as markup interfaces) 使用groovy基本上消除了实现接口的需要(除了标记接口之外)

In groovy you would just do this: 在groovy你会这样做:

def myObject = [m1: {-> doSomething()}] as TestInterface

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM