Java - eclipse 中的错误:赋值的左侧必须是一个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10743384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Error in eclipse: the left hand side of an assignment must be a variable
提问by Antifa
This is a small part of my code. My project is to simulate a whole school system. To add teachers, courses etc. All of my class members are private, so i created setters and getters methods. I try to give to 'teachersNum' a value and this must be automatic(not from keyboard). So i want to give it value 1 if its the first teacher etc. I hope you can understand. Sorry for my English.
这是我代码的一小部分。我的项目是模拟整个学校系统。添加教师、课程等。我所有的班级成员都是私人的,所以我创建了 setter 和 getter 方法。我尝试给 'teachersNum' 一个值,这必须是自动的(不是来自键盘)。所以我想给它值1,如果它是第一个老师等等。我希望你能理解。对不起我的英语不好。
public void addTeachersList(Teachers teachers) {
if(this.teachersSize<100){
this.teachersList[this.teachersSize] = teachers;
this.teachersList[this.teachersSize].getTeacherNum() = this.teachersSize -1;
this.teachersSize++;
}
}
回答by Marko Topolnik
You'll have to call a setter:
你必须调用一个setter:
this.teachersList[this.teachersSize].setTeacherNum(this.teachersSize-1);
Calling the getter getTeacherNum just gives you the number, it isn't a reference to that property.
调用 getter getTeacherNum 只会为您提供数字,它不是对该属性的引用。
Although I must say, you'd really do yourself a favor by using a List
implementation instead of arrays.
尽管我必须说,通过使用List
实现而不是数组,您真的会帮自己一个忙。
回答by Stephen P
In this line
在这一行
this.teachersList[this.teachersSize].getTeacherNum() = this.teachersSize -1;
getTeacherNum()
returns a value. You can't assign to it.
getTeacherNum()
返回一个值。你不能分配给它。
回答by Kumar Vivek Mitra
You have the problem here
你这里有问题
this.teachersList[this.teachersSize].getTeacherNum() = this.teachersSize -1;
.getTeacherNum() will return a value which must be stored in a variable on left side.
.getTeacherNum() 将返回一个必须存储在左侧变量中的值。
eg:
例如:
temp = .getTeacherNum();
温度 = .getTeacherNum();
And its better to use a static variable to keep the count of teachers, so every time a teacher is created he/she gets a nos which is different from the previous one
最好使用静态变量来保持教师的数量,因此每次创建教师时,他/她都会得到一个与前一个不同的 nos
eg:
例如:
xxxx001
xxxx002
xxxx003
回答by Kumar Vivek Mitra
You have the problem here
你这里有问题
this.teachersList[this.teachersSize].getTeacherNum() = this.teachersSize -1;
.getTeacherNum()
will return a value which must be stored in a variable on left side.
.getTeacherNum()
将返回一个必须存储在左侧变量中的值。
eg: temp = .getTeacherNum()
;
例如:temp = .getTeacherNum()
;
And its better to use a static variable to keep the count of teachers, so every time a teacher is created he/she gets a nos which is different from the previous one
最好使用静态变量来保持教师的数量,因此每次创建教师时,他/她都会得到一个与前一个不同的 nos
eg:
例如:
xxxx001
xxxx002
xxxx003