java 如何在.NET 中组织类似于#region/#endregion 的代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1492439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to organize the code similar to #region/#endregion in .NET?
提问by Niko Gamulin
While the code is growing big it is getting harder and harder to keep everything well organized. One thing I liked very much the time I developed in .NET was #region/#endregion which enabled to organize the code in logical groups and made further organization much easier.
虽然代码越来越大,但保持一切井井有条变得越来越难。我在 .NET 中开发时非常喜欢的一件事是#region/#endregion,它能够在逻辑组中组织代码并使进一步组织变得更加容易。
Does anyone know whether there is any similar code organization possible in Java?
有谁知道Java中是否有任何类似的代码组织可能?
采纳答案by Jason Miesionczek
That is a feature of Visual Studio, not .NET. You would have to look into your Java IDE of choice and see what options they have.
这是 Visual Studio 的功能,而不是 .NET。您必须查看您选择的 Java IDE 并查看它们有哪些选项。
回答by Dweep Sharma
This works in netbeans:
这适用于 netbeans:
// <editor-fold defaultstate="collapsed" desc=" Region Name ">
... Enter Code Block here ...
// </editor-fold>
回答by anonymous
回答by Nick
The problem with regions is they can make files filled with thousands of lines of spaghetti code look like they are compact, clean and well organized at first glance, which they are not.
区域的问题在于它们可以使充满数千行意大利面条式代码的文件乍一看看起来紧凑、干净且组织良好,但事实并非如此。
If a single file is getting unmanageable, think about how you've structured your classes and is there things you can refactor out into their own classes or methods?
如果单个文件变得无法管理,请考虑您是如何构建类的,是否可以将某些内容重构为它们自己的类或方法?
I got very region happy when I started .net and now I don't think I've written one in years. They lost all value in my eyes the first time I opened a file with 5 regions, thought, "Hmm...simple enough" only to expand one and get a few thousand lines of code that made no sense whatsoever.
当我开始使用 .net 时,我感到非常高兴,现在我想我已经很多年没有写过一篇了。当我第一次打开一个包含 5 个区域的文件时,他们在我眼中失去了所有价值,我想,“嗯......足够简单”只是扩展一个并得到几千行毫无意义的代码。
回答by Andrew Hare
No, Java doesn't have anything like that. If anything you should get a better text editor that allows for arbitrary code folding or code folding based on comments.
不,Java 没有这样的东西。如果有的话,您应该获得一个更好的文本编辑器,它允许基于注释的任意代码折叠或代码折叠。

