ロンボクいくつかの基本的な注釈を使用

ロンボク島は、単純なコードでは、Java開発で非常に便利なプラグインツール、レコードに、より一般的に使用されるノート、共有学習経験のいくつかのこのブログ。

使用ロンボクノートは、目的や役割嘘は、多くの場合、書き込みを繰り返している(などゲッター、セッター、コンストラクタ、など)いくつかのコードを記述する必要はありません。

まず、いくつかのノートを使用します。

  • @data
    このアノテーションを使用し、あなたが手ゲッター、セッター、等しく、canEqual、行かない hasCode、のtoString メソッドを、メモは自動的にコンパイル時にリストに追加されます。
  • @AllArgsConstructor
    コンストラクタを追加すると、すべての宣言のコンストラクタパラメータフィールドのプロパティを含む、使用されています
  • @NoArgsConstructor
    引数なしのコンストラクタを使用した後に作成
  • @Builder
    ビルダーについては、クラスのコンストラクタ内のアドレスにBuilderがある役割の一つは、例をたくさん持っている、それは思考のデザインモードで、トラブルコンストラクタの多くを書く必要がなくなり、より複雑である:内部クラスを使用します過剰な数のコンストラクタクラスを避けるために、オブジェクトをインスタンス化し、

その後、簡単なコード例によって:

1)まず、単純なクラスの確立、およびロンボクと注:これは注釈の前にコードがあることに注意し、注釈が生成されたコードを比較するバック転記することができ

@Data //生成getter,setter等函数
@AllArgsConstructor //生成全参数构造函数
@NoArgsConstructor//生成无参构造函数
@Builder
public class test1 {
    String name;
    String age;
    String sex;
}

2)試験は、入口:

 public static void main(String[] args) {
 //使用@Builder注解后,可以直接通过Builder设置字段参数
        test1 t1=new test1.test1Builder()
                .name("wang")
                .age("12")
                .sex("man")
                .build();
    System.out.println("name is"+t1.getName()+'\n'+"age is :"+t1.getAge());

}

3)クラスでコンパイル表示した後、前とノートを比較した後、コードの量は、書き込みに多くのコードを保存することが見つかりました:

public class test1 {
    String name;
    String age;
    String sex;
public static test1.test1Builder builder() {
    return new test1.test1Builder();
}

public String getName() {
    return this.name;
}

public String getAge() {
    return this.age;
}

public String getSex() {
    return this.sex;
}

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

public void setAge(String age) {
    this.age = age;
}

public void setSex(String sex) {
    this.sex = sex;
}

public boolean equals(Object o) {
    if (o == this) {
        return true;
    } else if (!(o instanceof test1)) {
        return false;
    } else {
        test1 other = (test1)o;
        if (!other.canEqual(this)) {
            return false;
        } else {
            label47: {
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name == null) {
                        break label47;
                    }
                } else if (this$name.equals(other$name)) {
                    break label47;
                }

                return false;
            }

            Object this$age = this.getAge();
            Object other$age = other.getAge();
            if (this$age == null) {
                if (other$age != null) {
                    return false;
                }
            } else if (!this$age.equals(other$age)) {
                return false;
            }

            Object this$sex = this.getSex();
            Object other$sex = other.getSex();
            if (this$sex == null) {
                if (other$sex != null) {
                    return false;
                }
            } else if (!this$sex.equals(other$sex)) {
                return false;
            }

            return true;
        }
    }
}

protected boolean canEqual(Object other) {
    return other instanceof test1;
}

public int hashCode() {
    int PRIME = true;
    int result = 1;
    Object $name = this.getName();
    int result = result * 59 + ($name == null ? 43 : $name.hashCode());
    Object $age = this.getAge();
    result = result * 59 + ($age == null ? 43 : $age.hashCode());
    Object $sex = this.getSex();
    result = result * 59 + ($sex == null ? 43 : $sex.hashCode());
    return result;
}

public String toString() {
    return "test1(name=" + this.getName() + ", age=" + this.getAge() + ", sex=" + this.getSex() + ")";
}

@ConstructorProperties({"name", "age", "sex"})
public test1(String name, String age, String sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
}

public test1() {
}

public static class test1Builder {
    private String name;
    private String age;
    private String sex;

    test1Builder() {
    }

    public test1.test1Builder name(String name) {
        this.name = name;
        return this;
    }

    public test1.test1Builder age(String age) {
        this.age = age;
        return this;
    }

    public test1.test1Builder sex(String sex) {
        this.sex = sex;
        return this;
    }

    public test1 build() {
        return new test1(this.name, this.age, this.sex);
    }

    public String toString() {
        return "test1.test1Builder(name=" + this.name + ", age=" + this.age + ", sex=" + this.sex + ")";
    }
}

}

概要:ロンボクノートは異なる注釈の役割を理解するために多くのことができ、使用することは非常に便利になります。
もう一つは、いくつかの関連ブログを投稿:
ロンボク島は、注釈機能のいくつかをインストールして使用します。
https://blog.csdn.net/motui/article/details/79012846
Javaのビルダー:
http://www.cnblogs.com/moonz-wu/archive /2011/01/11/1932473.html
https://www.cnblogs.com/begin1949/p/4930896.html

                                </div>
リリース5元の記事 ウォンの賞賛0 ビュー171

おすすめ

転載: blog.csdn.net/qq_44813090/article/details/104090989