Last active
December 17, 2015 02:39
-
-
Save YukiYoshikawa/5537769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package trial.yy.lombok.client; | |
import lombok.Data; | |
import static java.lang.System.out; | |
/** | |
* Created with Eclipse. | |
* User: yy | |
*/ | |
public class LombokDataClient { | |
/** | |
* MutableなBean | |
*/ | |
@Data | |
public static class Person { | |
/** 名前 */ | |
private String name; | |
/** 年齢 */ | |
private int age; | |
} | |
/** | |
* ImmutableなBean | |
*/ | |
@Data | |
public static class ImmutablePerson { | |
/** 名前 */ | |
private final String name; | |
/** 年齢 */ | |
private final int age; | |
} | |
public static void main(String[] args) { | |
Person person1 = new Person(); | |
// 対象クラスでsetterを記述してないのにsetterが使える!! | |
person1.setName("Taro"); | |
person1.setAge(12); | |
// 対象クラスでgetterを記述してないのにgetterが使える!! | |
out.println("name: " + person1.getName()); | |
out.println("age: " + person1.getAge()); | |
// toStringやhashCodeも自動生成されている | |
out.println("Person#toString: " + person1); | |
out.println("Person#hashCode: " + person1.hashCode()); | |
// メンバに同じ値を指定すればhashCodeは同じ値を返す | |
Person person2 = new Person(); | |
person2.setName("Taro"); | |
person2.setAge(12); | |
out.println("Person#hashCode: " + person2.hashCode()); | |
Person person3 = new Person(); | |
person3.setName("Taro"); | |
person3.setAge(90); | |
// equalsも自動生成されている | |
out.println("person1 eqauls person1 -> " + person1.equals(person1)); | |
out.println("person1 eqauls person2 -> " + person1.equals(person2)); | |
out.println("person1 eqauls person3 -> " + person1.equals(person3)); | |
out.println("####"); | |
// 対象クラスでメンバをfinalにすると自動的にコンストラクタが生成されている!! | |
ImmutablePerson immutablePerson1 = new ImmutablePerson("Jiro", 28); | |
// setterは存在しない状態(コンパイルエラー) | |
// immutablePerson1.setName("Taro"); | |
// immutablePerson1.setAge(12); | |
// 対象クラスでgetterを記述してないのにgetterが使える!! | |
out.println("name: " + immutablePerson1.getName()); | |
out.println("age: " + immutablePerson1.getAge()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment