【发布时间】:2017-05-08 18:58:54
【问题描述】:
我正在尝试制作一个定位不同城市的小程序作为我的第一个 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;
}
}
(我想将变量保留为私有)
这个类'Citiy'应该有坐标:
class City {
Gps where;
Location(int x, int y, int z) {
where.getX() = x;
where.getY() = y; //The Error Here
where.getZ() = z;
}
}
【问题讨论】:
-
ints 是私人的。创建设置器:setX(int x) {this.x = x;}然后使用where.setX(x);另外,你的类无论如何都不会编译 -
您需要使用 Setter 来更改
Gps的私有成员。
标签: java getter-setter