【发布时间】:2018-11-19 03:04:03
【问题描述】:
我有一个任务,我需要添加 2 个构造函数,并且除此之外还列出了变异函数。但是我认为大多数构造函数都是突变体:正如post 中所见。所以我很困惑我需要为 mutator 方法做什么?我目前有以下构造函数:
//Constructors
public Species() {
speciesName = "Balaenoptera musculus";
population = 15000;
growthRATE = -0.12;
if (endangered(growthRATE) == true) {
status = "endangered";
} else {
status = "not endangered";
}
}
public Species(String name, int populationSize, double GR) {
speciesName = name;
population = populationSize;
growthRATE = GR;
if (endangered(GR) == true) {
status = "endangered";
} else {
status = "not endangered";
}
}
public Species(Species species) {
speciesName = species.getSpeciesName();
population = species.getPopulation();
growthRATE = species.getGrowthRate();
if (endangered(growthRATE) == true) {
status = "endangered";
} else {
status = "not endangered";
}
}
基于 cmets:
这是我尝试过的,但它导致了以下错误
类型 PrintStream 中的方法 print(boolean) 不适用于参数 (无效)
在 main 的这一行:
Species O5 = O1;
System.out.print(O5.changeSpeciesName("Gorilla beringei"));
方法如下:
public void changeSpeciesName(String newSpeciesName) {
speciesName = newSpeciesName;
}
【问题讨论】:
-
没有。构造函数不是修改器。
-
那么什么是突变体的例子,因为我在我链接的帖子中发现的唯一一个是构造函数@ElliottFrisch
-
您链接到另一篇文章,但您真的阅读了接受的答案吗? ""Accessor" and "Mutator" are just fancy names fot a getter and a setter"
-
修改器改变现有对象,构造器初始化新对象。
标签: java