我正在尝试将uml表转换为实际代码。我已经完成了参数,getter和setter。但是我对方法和构造函数有些困难。
这就是这个类的UML:
(荷兰人的名字很抱歉)
这是我到目前为止所写的内容:
public class Shape {
private int shapeTeller;
protected String kleur;
protected Boolean gevuld;
public int getShapeTeller() {
return shapeTeller;
}
public void setShapeTeller(int shapeTeller) {
this.shapeTeller = shapeTeller;
}
public String getKleur() {
return kleur;
}
public void setKleur(String kleur) {
this.kleur = kleur;
}
public Boolean getGevuld() {
return gevuld;
}
public void setGevuld(Boolean gevuld) {
this.gevuld = gevuld;
}
public Shape() {
}
}
有人可以帮我写下剩下的代码吗?
答案 0 :(得分:1)
这是你遗失的构造函数:
public Shape(final String kleur, final boolean gevuld) {
this.kleur = kleur;
this.gevuld = gevuld;
}
这是方法声明(我不知道他们应该做什么):
public double getOmtrek() {
double omtrek = ... //doStuff to get omtrek
return omtrek;
}
public double getOppervlakte() {
double oppervlakte = ... //doStuff to get oppervlakte
return oppervlakte;
}
@Override
public String toString() {
//for example:
return "This shape has kleur: " + kleur + " and gevuld is " + gevuld;
}
答案 1 :(得分:1)
您只剩下构造函数部分。这一切都取决于调用特定构造函数时要执行的操作:
public Shape() {
// do something
// assign some default values
this.kleur = "white";
this.gevuld = false;
}
public Shape(String kleur, Boolean gevuld){
this.kleur = kleur;
this.gevuld = gevuld;
}