📁 ex01
☕ Gender.java
public enum Gender {
MALE("👦🏻"), FEMALE("👧🏼");
private String emoji;
Gender(String emoji) { this.emoji = emoji; }
public String getEmoji() { return emoji; }
}
☕ ChildClass.java
// 기존처럼 클래스로 작성해야 했다면...
public class ChildClass {
private final String name;
private final int birthYear;
private final Gender gender;
public ChildClass(String name, int birthYear, Gender gender) {
this.name = name;
this.birthYear = birthYear;
this.gender = gender;
}
public String getName() { return name; }
public int getBirthYear() { return birthYear; }
public Gender getGender() { return gender; }
}
☕ Child.java
// ⭐️ 레코드로 작성
public record Child(
String name,
int birthYear,
Gender gender
) {}
☕ Main.java
Child child1 = new Child("홍길동", 2020, Gender.MALE);
// 💡 toString 메소드 구현 (이후 배울 Object에서 상속받아 오버라이드)
String childStr = child1.toString();
Child[] children = new Child[] {
new Child("김순이", 2021, Gender.FEMALE),
new Child("이돌이", 2019, Gender.MALE),
new Child("박철수", 2020, Gender.MALE),
new Child("최영희", 2019, Gender.FEMALE),
};
for (Child child : children) {
System.out.printf(
"%s %d년생 %s 어린이%n",
child.gender().getEmoji(),
child.birthYear(),
child.name()
);
}
final
abstract
로 선언 불가private
, final