简体   繁体   English

在.java文件中编写Groovy代码

[英]Write Groovy code in .java file

I'm just new at Groovy and I'm following a simple tutorial. 我是Groovy的新手,我正在学习一个简单的教程。 I have this code: 我有这个代码:

class Example {
static void main(String[] args) {
  // Using a simple println statement to print output to the console
  println('Hello World');
}
}

but when I try it in my IDE, I got the error related to println . 但是当我在IDE中尝试它时,我得到了与println相关的错误。 I think that the Groovy syntax is not recognized. 我认为无法识别Groovy语法。 So, is there a way to write groovy code in a .java file? 那么,有没有办法在.java文件中编写groovy代码?

Simply: don't do that. 简单地说:不要那样做。

The file extension .java tells each and any reader (may that be a human reader or any kind of tooling): 文件扩展名.java告诉每个和任何读者(可能是人类读者或任何类型的工具):

in here, Java code to be found! 在这里,可以找到Java代码!

Thus: when you put anything into that file that is not Java, you are simply asking for trouble! 因此:当您将任何内容放入不是 Java的文件中时,您只是在寻找麻烦! (I have seen people doing such things, and you won't believe how much pain that caused on other people working on the same project; so on behalf of all the people around you: please bury this idea. Now! ) 看到有人在做这样的事情,你怎么也不会相信太多痛苦 ,造成了其他人在同一项目上工作,所以就代表着你身边所有的人。!请埋葬这个想法现在

You could look at this the other way round: Groovy is very close to Java, so if you think you have to "mix" things - see how far you get doing that within some groovy files! 你可以反过来看看这个:Groovy非常接近Java,所以如果你认为你必须“混合”东西 - 看看你在一些groovy文件中做到这一点有多远!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM