문제

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