样例
public enum Armor {
CHAIN_MAIL("Chain Mail"),
CLOTHES("Clothes"),
LEATHER("leather"),
PLATE("plate");
private String description;
Armor(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
}
public enum HairColor {
BLACK,
BLOND,
BROWN,
RED,
WHITE;
}
public enum HairType {
BALD("bald"),
CURLY("curly"),
LONG_CURLY("long curly"),
LONG_STRAIGHT("long straight"),
SHORT("short");
private String description;
HairType(String description) {
this.description = description;
}
}
public enum Profession {
MAGE,
PRIEST,
THIEF,
WARRIOR;
}
public enum Weapon {
AXE,
BOW,
DAGGER,
SWORD,
WARHAMMER;
}
public final class Hero {
private Armor armor;
private Weapon weapon;
private HairType hairType;
private HairColor hairColor;
private Profession profession;
public Hero(Builder builder){
this.armor = builder.armor;
this.weapon = builder.weapon;
this.hairType = builder.hairType;
this.hairColor = builder.hairColor;
this.profession = builder.profession;
}
public Armor getArmor() {
return armor;
}
public Weapon getWeapon() {
return weapon;
}
public HairType getHairType() {
return hairType;
}
public HairColor getHairColor() {
return hairColor;
}
public Profession getProfession() {
return profession;
}
public static class Builder {
private Armor armor;
private HairType hairType;
private HairColor hairColor;
private Profession profession;
private Weapon weapon;
private String name;
public Builder(Profession profession, String name) {
this.profession = profession;
this.name = name;
}
public Hero build() {
return new Hero(this);
}
Builder withArmor(Armor armor) {
this.armor = armor;
return this;
}
Builder withHairType(HairType hairType) {
this.hairType = hairType;
return this;
}
Builder withHairColor(HairColor hairColor) {
this.hairColor = hairColor;
return this;
}
Builder withWeapon(Weapon weapon) {
this.weapon = weapon;
return this;
}
}
}
public class Main {
public static void main(String[] args) {
var riobard = new Hero.Builder(Profession.MAGE, "Riobard").
withHairColor(HairColor.BLACK).
withWeapon(Weapon.DAGGER)
.build();
}
}