如何在Java注释中标记代码的逻辑部分? [英] How to mark logical sections of code in Java comments?

查看:232
本文介绍了如何在Java注释中标记代码的逻辑部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java类通常分为逻辑块。有标记这些部分的约定吗?



我个人使用这种方法:

  ////此处的名称//// 



例如,在Objective-C代码中,您可以使用此方法:

  #pragma mark  -  
#pragma mark此处的名称


$ b b

这将导致XCode中的菜单如下所示:



解决方案

我个人使用80个字符的行分隔符, / p>

  public class Client {

// ============= ==================================================== =================
//属性
// ==================== ==================================================== ==========

private String name;
private boolean checked;

// ======================================== ====================================
//构造函数
// =========================================== ===============================

public Client(){
}

public Client(String name,boolean checked){
this.name = name;
this.checked = checked;
}

// =================================== ===========================================
/ / accessors
// ========================================== ====================================

public String getName ){
return name;
}

public void setName(String name){
this.name = name;
}

public boolean isChecked(){
return checked;
}

public void setChecked(boolean checked){
this.checked = checked;
}

}

当然,对于这么小的POJO,但相信我,它证明非常有用的一些巨大的项目,我不得不浏览大的源文件,并快速找到我感兴趣的方法。它还有助于理解源代码结构。 p>

在Eclipse中,我创建了一组自定义模板(在Eclipse的首选项对话框中为Java - >编辑器 - >模板),用于生成这些栏。
- sepa(用于访问器的SEPARator)
- sepp(属性的SEParator)
- sepc(用于构造函数的SEParator)
- 等。



我还修改了标准的新类模板(Java - >代码样式 - > Eclipse首选项屏幕中的代码模板)



旧的Eclipse插件,名为 Coffee-bytes ,这增强了Eclipse折叠代码部分的方式。
我不知道它是否仍然有效,但我记得可以通过添加特殊注释,如// [SECTION]或某事定义任意可折叠区域。
它可能仍然在最近的Eclipse修订版中工作,所以看看。


Java classes are generally divided into logical "blocks". Is there a convention to mark these sections? Ideally, it would be supported by the major IDEs.

I personally use this method:

//// Section name here ////

However, some editors seem to have problems with this.

As an example, in Objective-C code you can use this method:

#pragma mark -
#pragma mark Section name here

This will result in a menu in XCode that looks like this:

解决方案

I personally use 80-chars line separators, like this :

public class Client {

    //================================================================================
    // Properties
    //================================================================================

    private String name;
    private boolean checked;

    //================================================================================
    // Constructors
    //================================================================================

    public Client() {
    }

    public Client(String name, boolean checked) {
        this.name = name;
        this.checked = checked;
    }

    //================================================================================
    // Accessors
    //================================================================================

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

}

Of course, this may seem a bit overkill for such a small POJO, but believe me, it proved very useful in some huge projects where I had to browse through big source files and quickly find the methods I was interested in. It also helps understand the source code structure.

In Eclipse, I have created a set of custom templates (Java -> Editor -> Templates in Eclipse's Preferences dialog) that generate those bars, eg. - sepa (SEParator for Accessors) - sepp (SEParator for Properties) - sepc (SEParator for Constructors) - etc.

I also modified the standard "new class" template (Java -> Code Style -> Code Templates in Eclipse Preferences screen)

Also, there is an old Eclipse plugin called Coffee-bytes, which enhanced the way Eclipse folds portions of code. I don't know if it still works, but I remembed one could define arbitrary foldable zones by adding special comments, like // [SECTION] or something. It might still work in recent Eclipse revisions, so take a look.

这篇关于如何在Java注释中标记代码的逻辑部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆