自己定义的学生类,其中包括语文,数学,英语成绩,并定义mix(总分)等于前三者之和。
虽然编译器没报错,但实际运行过程中会出现mix还是0的结果。
这个问题,困扰我很久了,虽然最后用mixget方法解决了但这个困惑还是不清楚,求解答!
附代码:
public class studentDate implements Comparable {
private String name;
private int chinese;
private int math;
private int English;
private int mix =chinese + math +English;
public studentDate() {
}
public studentDate(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
English = english;
this.mix = chinese + math +english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return English;
}
public void setEnglish(int english) {
English = english;
}
public int getMix() {
return mix;
}
public void setMix() {
this.mix = mix=this.chinese + this.math + this.English;
}
@Override
public int compareTo(Object o) {
int a = mix - this.mix;
if (a != 0) {
return a;
} else {
return a = 1;
}
}
}