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

问题描述 投票:0回答:1

我的环境: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

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

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

groovy include
1个回答
0
投票

在digit.groovy中,使用:

import AStar

如果它们在同一包中。

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

© www.soinside.com 2019 - 2024. All rights reserved.