包括一个groovy文件中的类 [英] including a class in a groovy file

查看:229
本文介绍了包括一个groovy文件中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我得到下面的错误

$ b $($)

我试图添加一个类(从一个groovy文件切割并粘贴到另一个文件) b

Groovy脚本中的错误:启动失败,Script1.groovy:101:类定义不在此处。可能尝试使用内部类。不支持内部类,也许尝试使用闭包。



目标是分离整个静态逻辑。

解决方案

你看起来像一个内部

 类地图

在方法中间 p>

  public static void populateInstance(List lookup_1,List lookup_2,List lookup_3,List lookup_4,
List lookup_5,List lookup_6,List lookup_7){






另外,由于这是Groovy,宣布所有这些获得者不应该是必要的








 import com.mobRet。* 

你已经在这个包里。还有,

  import java.util.ArrayList 
import java.util.HashMap;
import java.util.List

可以在Groovy默认导入它们时将其删除 p>




populateInstance 方法中,您可以更改所有这些:

  tariffAllowance = new ArrayList(); 
lookup_1.each {offer - >
tariffAllowance.add(offer)
}

fmb = new ArrayList();
lookup_2.each {offer - >
fmb.add(优惠)
}

阻止:

  tariffAllowance = lookup_1.collect()
fmb = lookup_2.collect()






您似乎也在通过调用<$ c来创建一个可以更改所有数据的单例$ C> populateInstance 。在多线程环境中,这种错误的可能性非常大(实际上这是一个巨大的全局变量)。

I have tried to include a class(literally cut and pasted from one groovy file to another).

I get the below error

Error(s) in Groovy Script: startup failed, Script1.groovy: 101: Class definition not expected here. Possible attempt to use inner class. Inner classes not supported, perhaps try using a closure instead.

The objective is to separate the whole static logic separately.

解决方案

You've got what looks like an internal class

    class Maps

Right in the middle of the method

public static void populateInstance(List lookup_1, List lookup_2, List lookup_3, List lookup_4, 
                                    List lookup_5, List lookup_6, List lookup_7){


Also, as this is Groovy, declaring all those getters shouldn't be necessary


And

import com.mobRet.*

isn't needed as you're already in that package. Also

import java.util.ArrayList
import java.util.HashMap;
import java.util.List

Can be removed as Groovy imports them by default


And inside the populateInstance method, you can change all those:

    tariffAllowance = new ArrayList();
    lookup_1.each { offer ->
        tariffAllowance.add(offer)
    }

    fmb = new ArrayList();
    lookup_2.each { offer ->
        fmb.add(offer)
    }

blocks to:

    tariffAllowance = lookup_1.collect()
    fmb = lookup_2.collect()


You also seem to be making a singleton that can have all of it's data changed by calling populateInstance. In a multi-threaded environment, the chances of this being wrong are huge (effectively this is one huge global variable)

这篇关于包括一个groovy文件中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆