我正在入门Java类中编写一个主题为层次结构的作业。基类或超类是紧固件,钉子,螺丝,螺母和螺栓是其子类。其中一些班级也有自己的子班级。分配中的一条重要规则是,所有属性都必须是私有的,并且不允许访问器。
分配的目标之一是要有一个toString方法,该方法返回一个由特定紧固件的属性组成的字符串。每个类都有一个toString方法,并使用super.toString
引用父类返回的字符串部分。
一个前端子类(呈现给用户的类)的最终输出将是:Wood Screw, Sharp point, Flat head, Square drive, 0.75" long, #8-15 thread, Steel, with a Zinc finish. 750 in a unit, $44.1 per unit.
下面是WoodScrew,这是Screw的分支。由于某种原因,我为WoodScrew获得的输出为0.75" long, #8-15 thread, assignmentFour.WoodScrew@1e802ef9
,而不是上面的内容。所有此类子类都在发生这种问题。
我怀疑此问题与将枚举值转换为字符串一并返回该字符串有关,但我不确定。父类中的大多数toString()方法都使用从枚举值派生的字符串。
对于我如何解决此问题,我将不胜感激。
public class WoodScrew extends Screw implements Serializable {
private Enum<ScrewDesigns.WoodScrewPoints> woodPoint;
public WoodScrew(double len, Threads diam, Enum<Materials.ThreadedMaterials> mtrl,
Enum<Finishes.ScrewFinish> finish, Enum<ScrewDesigns.ScrewHeads> head,
Enum<assignmentFour.ScrewDesigns.ScrewDrive> drive,
Enum<ScrewDesigns.WoodScrewPoints> point, double untPrice, int numPerUnit)
throws IllegalFastener {
super(len, diam, mtrl, finish, head, drive, untPrice, numPerUnit);
String pointStr = point.name();
setPoint(point, pointStr);
toString(pointStr);
}
private void setPoint(Enum<ScrewDesigns.WoodScrewPoints> point, String pointStr) throws IllegalFastener{
if ((!pointStr.matches("Double_Cut|Sharp|Type_17")) || point == null) {
throw new IllegalFastener("Illegal point value: " + pointStr);
}
woodPoint = point;
}
public String toString(String pointStr) {
return("Wood Screw, " + pointStr + " point, ");
}
}