我有一个名为Box和PackageBox的蹩脚测试类。 Box使用Builder Pattern和PackageBox,它应该扩展Box
这是一些代码
package console.app.model;
public class Box {
private String name = "";
private int weight = 0;
private int width = 0;
private int height = 0;
// Box instance for comparison on method equals().
private static Box comparisonBox;
public static class Builder {
private String name = "";
private int weight = 0;
private int width = 0;
private int height = 0;
public Builder(String name) {
this.name = name;
}
public Builder weight(int weight) {
this.weight = weight;
return this;
}
public Builder width(int width) {
this.width = width;
return this;
}
public Builder height(int height) {
this.height = height;
return this;
}
public Box build() {
return new Box(this);
}
}
private Box(Builder builder) {
name = builder.name;
weight = builder.weight;
width = builder.width;
height = builder.height;
}
// Setters and getters, etc.
}
如何将Box扩展到PackageBox? 谢谢,如果出现问题,请在Box类中告诉我或要替换的内容。
答案 0 :(得分:0)
向Box添加一个受保护的构造函数,然后将其子类化,从PackageBox构造函数中调用受保护的构造函数。但是,如果要使用该路径,则需要为PackageBox实现新的Builder。