我是一个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() {
}
。
如何实现这个构造函数?
您只需在构造函数中创建一个新的WidthLength
对象。
public Rectangle(double width, double length, String col)
{
widthLength = new WidthLength(width, length);
color = col;
}
try this:
public Rectangle(double width, double length, String color) {
this.widthLength = new WidthLength(width, length);
this.color = color;
}
public WidthLength getWidthLength() {
return this.widthLength;
}