JAVA代码报错,不知道错在哪里,有没有大神帮帮忙看一下!

2024-11-18 18:33:59
推荐回答(1个)
回答1:

构造方法和类名不一致,修改如下:

public class Students {
int id = 0;
static int studentNum = 0;

Students(int a) {
id = a;
studentNum++;
}

public static void main(String[] args) {
Students s1 = new Students(1);
Students s2 = new Students(2);
Students s3 = new Students(3);
Students s4 = new Students(4);

System.out.println(s1.id);
System.out.println(s2.id);
System.out.println(s3.id);
System.out.println(s4.id);
System.out.println(Students.studentNum);
}

}