Groovy脚本文件中的静态main方法

本文介绍Groovy中的静态main方法。先看如下代码:

<ol class="dp-j">
<li class="alt"><span><span class="keyword">class</span><span> Test1 {     </span></span></li>
<li class="">
<span>    </span><span class="keyword">public</span><span> Test1() {     </span>
</li>
<li class="alt">
<span>         println </span><span class="string">"TEST1"</span><span>     </span>
</li>
<li class=""><span>     }     </span></li>
<li class="alt"><span>}     </span></li>
<li class=""><span>     </span></li>
<li class="alt">
<span></span><span class="keyword">class</span><span> Test2 {     </span>
</li>
<li class="">
<span>    </span><span class="keyword">public</span><span> Test2() {     </span>
</li>
<li class="alt">
<span>         println </span><span class="string">"TEST2"</span><span>     </span>
</li>
<li class=""><span>     }     </span></li>
<li class="alt">
<span>    </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(args) {     </span>
</li>
<li class="">
<span>        </span><span class="keyword">new</span><span> Test1()     </span>
</li>
<li class="alt"><span>     }     </span></li>
<li class=""><span>}    </span></li>
</ol>

此代码不论用groovy命令行还是用groovyConsole来运行都要出错,好像是引用

groovy.lang.MissingMethodException: No signature of method: Test1.main() is applicable for argument types: ([Ljava.lang.String;) values: {[]}
at Test1.invokeMethod(Script0)

再来看看新的代码:

<ol class="dp-j">
<li class="alt"><span><span class="keyword"><strong><font color="#006699">class</font></strong></span><span> Test2 {</span><span class="comment"><font color="#008200">//含有static void main的方法的Test2必须要在第一个定义    </font></span><span> </span></span></li>
<li class="">
<span>    </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> Test2() {     </span>
</li>
<li class="alt">
<span>         println </span><span class="string"><font color="#0000ff">"TEST2"</font></span><span>     </span>
</li>
<li class=""><span>     }     </span></li>
<li class="alt">
<span>    </span><span class="keyword"><strong><font color="#006699">static</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">void</font></strong></span><span> main(args) {     </span>
</li>
<li class="">
<span>        </span><span class="keyword"><strong><font color="#006699">new</font></strong></span><span> Test1()     </span>
</li>
<li class="alt"><span>     }     </span></li>
<li class=""><span>}     </span></li>
<li class="alt">
<span></span><span class="keyword"><strong><font color="#006699">class</font></strong></span><span> Test1 {     </span>
</li>
<li class="">
<span>    </span><span class="keyword"><strong><font color="#006699">public</font></strong></span><span> Test1() {     </span>
</li>
<li class="alt">
<span>         println </span><span class="string"><font color="#0000ff">"TEST1"</font></span><span>     </span>
</li>
<li class=""><span>     }     </span></li>
<li class="alt"><span>}    </span></li>
</ol>

其中的含义不用多说了吧:

相关推荐