我是一名java初学者,希望能够帮助实现我的构造函数
例如
public Class WidthLenth {
private double width;
private double length;
public WidthLength(double width, double length) {
this.width = width;
this.length = length;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
并在另一个班级
public Class Rectangle {
private WidthLength widthLength <-- there is a uni-directional relationship here
private String color
我需要构造函数采用这种格式
public Rectangle(double width, double length, String color) {
}
所以方法
public getWidthLenth() {
}
会起作用。
我该如何实现这个构造函数?
答案 0 :(得分:0)
您只需在构造函数中创建WidthLength
的新对象。
public Rectangle(double width, double length, String col)
{
widthLength = new WidthLength(width, length);
color = col;
}
答案 1 :(得分:0)
试试这个:
public Rectangle(double width, double length, String color) {
this.widthLength = new WidthLength(width, length);
this.color = color;
}
public WidthLength getWidthLength() {
return this.widthLength;
}