Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在groovy中从另一个类调用方法?_Groovy_Spock_Geb - Fatal编程技术网

如何在groovy中从另一个类调用方法?

如何在groovy中从另一个类调用方法?,groovy,spock,geb,Groovy,Spock,Geb,例如,我有一个这样的类: class firstOne{ .... def A (){ } } class secondOne{ // I need to call and use method A from class firstOne // even I get error if I try to follow Java like calls // firstOne method = new firstOne(); // me

例如,我有一个这样的类:

class firstOne{

    ....
    def A (){

    }

}

class secondOne{

    // I need to call and use method A from class firstOne
    // even I get error if I try to follow Java like calls
    // firstOne method = new firstOne();
    // method.A()   
}

我已经试过了,但是没有办法。任何类型的建议或示例都会非常有用。

这不是Groovy/Grails特有的:

firstOne first = new firstOne()
first.A()

您还应该大写类的第一个字母,而不是方法(这是Java中的最佳实践)。

我看不出这有什么问题:

class FirstOne {

    def a() {
        println "a"
    }
}

class SecondOne {

    def b() {
        new FirstOne().a()
        println "b"
    }
}

new FirstOne().a()
println("")
new SecondOne().b()
输出:

a
a
b

这与grails有什么关系?此外,最好避免使用大写字母方法。您会遇到什么错误?您的语句需要位于代码块而不是类块中。它位于geb自动化脚本中。它显示了一个常规错误:“org.spockframework.runtime.ConditionNotSatisfiedError”。我将在几分钟内发布有关错误的更多详细信息。我已经尝试过了,但没有成功!这不是100%的Groovy/Grails,而是完全使用Java+Groovy@S.M.AlMamun那么你的问题少了一部分。我知道你已经对包含更多细节发表了评论,所以也许这些会有所帮助。谢谢你,伙计,这对解决我的问题是有益的!