我有动物课...
public abstract class Animal {
private AnimalType type;
private String noun;
private String scientificNoun;
private short minSizeCm;
private short maxSizeCm;
private double minWeightGrams;
private double maxWeightGrams;
private Set<AnimalColour> animalColour = new HashSet<>();
private Set<AnimalLocomotion> locomotion = new HashSet<>();
private Set<AnimalCountry> country = new HashSet<>();
private Set<AnimalNaturalHabitat> naturalHabitat = new HashSet<>();
public Animal(AnimalType type, String noun, String scientificNoun, short minSizeCm, short maxSizeCm,
double minWeightGrams, double maxWeightGrams, Set<AnimalColour> animalColour,
Set<AnimalLocomotion> locomotion, Set<AnimalCountry> country,
Set<AnimalNaturalHabitat> naturalHabitat) {
this.type = type;
this.noun = noun;
this.scientificNoun = scientificNoun;
this.minSizeCm = minSizeCm;
this.maxSizeCm = maxSizeCm;
this.minWeightGrams = minWeightGrams;
this.maxWeightGrams = maxWeightGrams;
this.animalColour = animalColour;
this.locomotion = locomotion;
this.country = country;
this.naturalHabitat = naturalHabitat;
}
}
我也有一个Bird类...
public abstract class Bird extends Animal {
private BirdBeakShape beakShape;
private Set<AnimalColour> featherColour;
private short minWingspanLengthCm;
private short maxWingspanLengthCm;
public Bird(String noun, String scientificNoun, short minSizeCm, short maxSizeCm,
double minWeightGrams, double maxWeightGrams, Set<AnimalColour> animalColour,
Set<AnimalLocomotion> locomotion, Set<AnimalCountry> country,
Set<AnimalNaturalHabitat> naturalHabitat, BirdBeakShape beakShape, Set<AnimalColour> featherColour,
short minWingspanLengthCm, short maxWingspanLengthCm) {
super(AnimalType.BIRD, noun, scientificNoun, minSizeCm, maxSizeCm, minWeightGrams, maxWeightGrams, animalColour,
locomotion, country, naturalHabitat);
this.beakShape = beakShape;
this.featherColour = featherColour;
this.minWingspanLengthCm = minWingspanLengthCm;
this.maxWingspanLengthCm = maxWingspanLengthCm;
}
}
还有一个Buzzard
类...
import java.util.Set;
public class Buzzard extends Bird {
public Buzzard(String noun, String scientificNoun, short minSizeCm, short maxSizeCm,
double minWeightGrams, double maxWeightGrams, Set<AnimalColour> animalColour,
Set<AnimalLocomotion> locomotion, Set<AnimalCountry> country,
Set<AnimalNaturalHabitat> naturalHabitat, BirdBeakShape beakShape, Set<AnimalColour> featherColour,
short minWingspanLengthCm, short maxWingspanLengthCm) {
super("Buzzard", "Buteo buteo", (short) 51, (short) 57, 550, 1300,
AnimalColour.BROWN);
}
}
我正在尝试在构造函数中设置Buzzard的不同颜色。像棕色,黑色,灰色。颜色枚举是...
public enum AnimalColour {
BROWN,
BLACK,
WHITE,
GREY,
GREY_BROWN,
BLACK_STREAKS,
GREEN,
CREAM
}
但是,我不知道如何在构造函数中将枚举中的各种颜色设置为一个集合(或者赞赏任何更好的实现方式)。此外,还有很多重复。有什么办法可以减少这种情况?因为我最终还将拥有哺乳动物和爬行动物类,所以即使现在重复的代码也太多了
答案 0 :(得分:2)
Buzzard
首先不应是一门课。它应该是Bird
的实例。您为其提供的字段值太具体而无法成为一个类(除非Buzzard
的某些部分未在此处显示)。
您应该删除Buzzard
类,并创建名为Bird
的{{1}}实例:
buzzard
您也可以考虑使用builder pattern。