Why is my constructor not applying the variables?:

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";
}
   }

What is missing here?

有帮助吗?

解决方案

Change

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";
    }

To

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;
    }

You are not using the values passed to the constructor.

其他提示

These lines...

s=9;
street="Street";

...assign values to the local variables corresponding to the arguments of your constructor, while you want to assign them to the fields instead, like this:

this.s = s;
this.street = street; // or "Street", if you prefer

The this. prefix makes the compiler understand that you are referring to the field, and not to the local variable of the same name.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top