为什么Student8方法里面的形参可有可无,我都能正常输出


public class ExerTest3{
    public static void main(String[] args) {
        Student8[] test = new Student8[20];
         for(int i = 0;i < test.length;i++) {
             test[i] = new Student8();
             test[i].number = i+1;
             test[i].state = (int)(Math.random()*6+1);
             test[i].score = (int)(Math.random()*101+1);
             test[i].info(test);
         }
        
    }
} 
class Student8 {
    int number,state,score;
    
    public void info(Student8[] test ) {
        System.out.println("学生学号:" + number + "  学生年级:" + state
                + " 学生成绩: " + score);
    }
    
}

img

函数中你没有使用参数啊。你要加个循环输出参数中的所有信息

 
public class ExerTest3{
    public static void main(String[] args) {
        Student8[] test = new Student8[20];
         for(int i = 0;i < test.length;i++) {
             test[i] = new Student8();
             test[i].number = i+1;
             test[i].state = (int)(Math.random()*6+1);
             test[i].score = (int)(Math.random()*101+1);
             test[i].info(test);
         }
        
    }
} 
class Student8 {
    int number,state,score;
    public void info(Student8[] test ) {
        for(int i = 0;  i < test.length;  i++) {
              System.out.println("学生学号:" + test[i].number + "  学生年级:" +  test[i].state
                + " 学生成绩: " +  test[i].score);
       }
} 
}

因为你的形参test,压根在函数里面没用到,可以这样试试


public void info(Student8[] test ) {
  for(int i = 0;i < test.length;i++) {
System.out.println("学生学号:" + test[i].number + "  学生年级:" +  test[i].state
                + " 学生成绩: " +  test[i].score);

}
            }


你的info方法都没有用到test变量,也就是说Student8[] test这个形参你没有使用,所有前后两次输出都是一样的

因为形参可以不用赋值,有值说明声明了一个固定长度的数组,没有值表明声明了一个没有长度的数组,不影响Student8对象的创建

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632