如何在另一个groovy文件中包含一个groovy脚本(带有类,函数)?

时间:2020-06-13 11:20:31

标签: groovy include

我的环境:groovy -v

Groovy版本:3.0.4 JVM:1.8.0_242供应商:Azul Systems,Inc.操作系统:Linux

我写了两个文件:AStar.groovy和digit.groovy。

像这样归档AStar.groovy:

class State{
    int deep = 0;
    def pre;
    double f = 0;
}

abstract class AStar {
    State begin;
    State end;
    def astar() {
        // some astar code
    }
    abstract def judge(def state);
}

和文件digit.groovy一样:

class DigitState extends State {
    // some fields and methods
}
class Digit extends AStar {
    // some fields and methods;
    def judge(def state) {
        // some codes.
    }
}
def digit = new Digit();
def path = digit.astar();
path.each {
    println(it);
}

现在我要将数字运行为:groovy digit.groovy

但是它告诉我“无法解析类状态”

我看到以下网址: Including a groovy script in another groovy

但是我不能以这种方式运行我的脚本,而且我不知道我的错是什么。

如何在不编译的情况下包含文件?

1 个答案:

答案 0 :(得分:0)

在digit.groovy中,使用:

import AStar

如果它们在同一包装中。

我在Eclipse中尝试了您的代码,即使没有导入语句,它也可以运行。