简体   繁体   English

java中的嵌套多行注释

[英]nested multiline comments in java

While writing experimental code, I find it very useful to comment out entire blocks of code at a time. 在编写实验代码时,我发现一次注释掉整个代码块非常有用。 However, i can't find a reasonable way to do this in Java, because I frequently end up with nested blocks being commented out. 但是,我无法找到一种合理的方法在Java中执行此操作,因为我经常最终将嵌套的块注释掉。

In C or C#, this is easily achieved using #if 0 or #if false 在C或C#中,使用#if 0#if false可以轻松实现

I can't seem to find any such equivalent in Java -- any help would be appreciated. 我似乎无法在Java中找到任何这样的等价物 - 任何帮助都会受到赞赏。 Thanks! 谢谢!

I'm presuming you are wanting to comment out nested code for debugging or testing purposes, right? 我假设您想要将嵌套代码注释掉以进行调试或测试,对吧? Because leaving huge blocks of commented out code in production code is generally considered very bad style. 因为在生产代码中留下大量注释掉的代码通常被认为是非常糟糕的风格。

Nested comments is not natively a feature of java. 嵌套注释本身不是java的一个特性。 So what can you do? 所以,你可以做什么? Here are some different options: 以下是一些不同的选择:

Slash Slash comment nesting The best I've been able to come up with is to use an editor that has a hotkey to slashslash comment out an entire block of code as //'s can be nested. Slash Slash注释嵌套我能够想到的最好的方法是使用一个带有热键的编辑器来斜杠,注释掉整个代码块,因为//可以嵌套。 Eclipse and Netbeans both have this support, though there are minor differences in how it works between IDEs, as far as when it decides to toggle the comments vs nest them. Eclipse和Netbeans都有这种支持,尽管它在IDE之间的工作方式上存在细微差别,只要它决定切换注释和嵌套它们。 Usually nesting // comments can be done so long as the selections are not identical. 通常嵌套//注释可以在选择不相同的情况下完成。

third party preprocessor That said, J2ME uses a pre-processor in eclipse, but I don't think that would help with desktop java. 第三方预处理器那就是说,J2ME在eclipse中使用了预处理器,但我不认为这对桌面java有帮助。 There do appear to be other preprocessors written for or compatible with eclipse if you search for them. 如果你搜索它们,似乎有其他预处理器为eclipse编写或兼容。 However, that definitely would hinder portability as that's not a standard java feature. 但是,这肯定会妨碍可移植性,因为这不是标准的java功能。

modify comment Another thing I've often done for a quick nested comment need (moreso in css which doesn't do // comments than java though) is to add a space between the final two characters ending the inner comment */ so that java will not end the outer comment at the inner comment's end. 修改注释我经常为快速嵌套注释需要做的另一件事(更多的是在css中不做//注释而不是java)是在结束内部注释的最后两个字符之间添加一个空格*/以便java不会在内部评论的结尾处结束外部评论。 Not too difficult (but slightly manual) to restore the original comment. 恢复原始评论并不太困难(但略微手动)。 The nested /* is usually ignored by the parser based on how it does its pattern matching. 解析器通常会根据它的模式匹配方式忽略嵌套的/* Eg /* code /* nested comment * / more code */` 例如/ * code / *嵌套注释* /更多代码* /`

use version control You could also easily use most any version control system to save a copy of your code with the code to remove. 使用版本控制您还可以轻松地使用大多数版本控制系统来保存代码的副本以及要删除的代码。 And then later use the version control restore/diff/merge to get your removed code back. 然后使用版本控制restore / diff / merge来恢复删除的代码。 Generally this solution is considered the best style. 通常这种解决方案被认为是最好的风格。 Especially if this commented out code will be needed to be left that way for long amounts of time. 特别是如果这个注释掉的代码需要长时间保留。

if (false) If your code that you want to mass prevent from running both compiles and is within a single function, you can easily disable it by adding an if statement around it that the condition will never be true such as if (false) { ... } Obviously, this would never pass any sort of lint type code inspection tool, but its quick and easy ;-) if(false)如果您希望批量生成的代码无法同时运行并且在单个函数内,则可以通过在其周围添加if语句来轻松禁用它,条件永远不会为真,例如if (false) { ... }显然,这绝不会通过任何类型的lint类型代码检查工具,但它的快速简便;-)

Comments are not meant for leaving dead code - they are meant to describe live code. 注释不是为了留下死代码 - 它们用于描述实时代码。

For the rare cases that this is needed, using an IDE to comment the lines that you want ( CTRL + / in eclipse) is sufficient. 对于极少数情况需要这样做,使用IDE来注释你想要的行( CTRL + /在eclipse中)就足够了。

If this is a common practice, rethink your process and your code. 如果这是一种常见做法,请重新考虑您的流程和代码。 And feel free to delete code and commit it to SCM. 并随意删除代码并将其提交给SCM。 It won't get lost if you need it. 如果你需要,它不会迷路。

You could just to if(false){}. 你可以去if(false){}。 Java doesn't really percompile the way C or C++ does but the Java compiler should be smart enough to skip that entire section (once it's determined that this if statement will never evaluate to true). Java并没有真正按照C或C ++的方式进行编译,但Java编译器应该足够聪明以跳过整个部分(一旦确定此if语句永远不会评估为true)。 This will only work to comment out code within functions. 这仅用于注释函数内的代码。

Or, if you have a good IDE like eclipse or notepad++ you can use the built-in shortcut keys to comment out multiple lines using //. 或者,如果您有一个像eclipse或notepad ++这样的好IDE,您可以使用内置快捷键来使用//注释掉多行。

Eclipse = ctrl + / (repress to uncomment) N++ = ctrl + k (shift + crtl + k to uncomment) Eclipse = ctrl + /(压制取消注释)N ++ = ctrl + k(shift + crtl + k取消注释)

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

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