Java-赋值的左侧必须是变量

Java-赋值的左侧必须是变量,java,getter-setter,Java,Getter Setter,作为我的第一个Java项目,我正在尝试制作一个定位不同城市的小程序 我想从“City”类访问我的类“GPS”的变量,但我一直得到这个错误:作业的左侧必须是变量。任何人都可以向我解释我在这里做错了什么,以及今后如何避免这样的错误 public class Gps { private int x; private int y; private int z; public int getX() { return this.x; } public int ge

作为我的第一个Java项目,我正在尝试制作一个定位不同城市的小程序

我想从“City”类访问我的类“GPS”的变量,但我一直得到这个错误:作业的左侧必须是变量。任何人都可以向我解释我在这里做错了什么,以及今后如何避免这样的错误

public class Gps {
  private int x;
  private int y;
  private int z;

   public int getX() {
    return this.x; 
   }

   public int getY() {
    return this.y; 
   }

   public int getZ() {
    return this.z; 
   }
}
(我想将变量保留为private)

这类“City”应该有以下坐标:

class City {
  Gps where;

   Location(int x, int y, int z) {
     where.getX() = x;
     where.getY() = y;    //The Error Here
     where.getZ() = z;
   }
}

错误本身就说明了这一点:不能为非字段或变量的对象赋值。getter用于获取类中存储的值。Java使用setter将值存储回:

public int getX() {
    return x; 
}
public void setX(int x) {
    this.x = x;
}
现在可以通过调用setter设置值:

City(int x, int y, int z) {
    where.setX(x);
    ...
}
然而,这种解决方案并不理想,因为它使
Gps
可变。您可以通过添加构造函数使其保持不变:

public Gps(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
}
现在
City
可以一次设置
where

City(int x, int y, int z) {
    where = new Gps(x, y, z);
}

错误本身就说明了这一点:不能为非字段或变量的对象赋值。getter用于获取类中存储的值。Java使用setter将值存储回:

public int getX() {
    return x; 
}
public void setX(int x) {
    this.x = x;
}
现在可以通过调用setter设置值:

City(int x, int y, int z) {
    where.setX(x);
    ...
}
然而,这种解决方案并不理想,因为它使
Gps
可变。您可以通过添加构造函数使其保持不变:

public Gps(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
}
现在
City
可以一次设置
where

City(int x, int y, int z) {
    where = new Gps(x, y, z);
}

不要用getter设置属性。应该这样做:

public class Gps {
    private int x;
    private int y;
    private int z;

    public int getX() {
        return this.x; 
    }

    public int getY() {
        return this.y; 
    }

    public int getZ() {
        return this.z; 
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setZ(int z) {
        this.z = z;
    }
}


class City {
    Gps where;

    City(int x, int y, int z) {
       this.where = new Gps();
       where.setX(x);
       where.setY(y);
       where.setZ(z);
    }
}

不要用getter设置属性。应该这样做:

public class Gps {
    private int x;
    private int y;
    private int z;

    public int getX() {
        return this.x; 
    }

    public int getY() {
        return this.y; 
    }

    public int getZ() {
        return this.z; 
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setZ(int z) {
        this.z = z;
    }
}


class City {
    Gps where;

    City(int x, int y, int z) {
       this.where = new Gps();
       where.setX(x);
       where.setY(y);
       where.setZ(z);
    }
}

int
s是私有的。创建setter:
setX(intx){this.x=x;}
然后使用
where.setX(x)
此外,您的类无论如何都不会编译您需要使用Setter来更改
Gps
的私有成员。
int
是私有的。创建setter:
setX(intx){this.x=x;}
然后使用
where.setX(x)
另外,你的类无论如何都不会编译。你需要使用Setter来更改
Gps
的私有成员。这对我很有效。谢谢你的帮助。我可以问最后一个问题吗,如果我想通过城市内部的一个Getter呼叫GPS的x怎么办@Baldr您可以通过通常的方式访问getter-例如,
public int getX(){return where.getX();}
另一种方法是发布
where
-例如,
public Gps getWhere(){return where;}
调用者将得到这样的X:
myCity.getWhere().getX()
这对我很有效。谢谢你的帮助。我可以问最后一个问题吗,如果我想通过城市内部的一个Getter呼叫GPS的x怎么办@Baldr您可以通过通常的方式访问getter-例如,
public int getX(){return where.getX();}
另一种方法是发布
where
-例如,
public Gps getWhere(){return where;}
调用者将像这样获得X:
myCity.getWhere().getX()