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>
其中的含义不用多说了吧:
相关推荐
软件的信雅达 2020-11-02
糖葫芦娃哈哈 2020-11-02
淡茶 2020-05-10
PHP学习笔记 2020-03-06
anvien 2020-01-08
quzhongwei 2020-01-06
淡茶 2020-01-03
tysforwork 2019-12-12
简单点好 2013-09-04
PeterHao0 2013-08-31
软件的信雅达 2019-11-19
软件的信雅达 2017-02-11
yiyilanmei 2015-04-28
无聊找点事做 2019-09-06
HaleyJenkins 2016-10-18
PeterHao0 2016-01-22
dieefer 2017-02-11
liushidexing 2016-11-03
春天花会开 2016-11-01