为什么我的构造函数不应用变量?:
导入 java.awt.geom.Point2D;
公共类 Waypoint 扩展 Point2D.Double{
private double s;
private String street;
public Waypoint(double x, double y, double s, String street) {
super(x,y);
s=9;
street="Street";
}
}
这里缺少什么?
为什么我的构造函数不应用变量?:
导入 java.awt.geom.Point2D;
公共类 Waypoint 扩展 Point2D.Double{
private double s;
private String street;
public Waypoint(double x, double y, double s, String street) {
super(x,y);
s=9;
street="Street";
}
}
这里缺少什么?
改变
import java.awt.geom.Point2D;
public class Waypoint extends Point2D.Double{
private double s;
private String street;
public Waypoint(double x, double y, double s, String street) {
super(x,y);
s=9;
street="Street";
}
到
import java.awt.geom.Point2D;
public class Waypoint extends Point2D.Double{
private double s;
private String street;
public Waypoint(double x, double y, double s, String street) {
super(x,y);
this.s = s;
this.street = street;
}
您没有使用传递给构造函数的值。
这些线...
s=9;
street="Street";
...将值分配给与构造函数的参数相对应的局部变量,而您希望将它们分配给字段,如下所示:
this.s = s;
this.street = street; // or "Street", if you prefer
前缀使编译器明白您指的this.
是字段,而不是同名的局部变量。