xzdjsnb 2022-03-27 10:42 采纳率: 92.2%
浏览 33
已结题

没明白 ExerTest1 test = new ExerTest1();这么操作的含义是什么,我上面stus new对象为什么也在main方法中new,脑海中很混乱,


public class ExerTest1 {
      public static void main(String[] args) {
          //声明student类型的数组
          Student1[] stus = new Student1[20];
          for(int i = 0;i < stus.length;i++) {
              //给数组元素赋值
              stus[i] = new Student1();
              //给Student对象的属性赋值
              stus[i].number = (i + 1);
              //年级[1-6];
              stus[i].state = (int)(Math.random()*(6-1+1)+1);
              //成绩[0-100];
              stus[i].score = (int)(Math.random()*(100-0+1));
          }
          ExerTest1 test = new ExerTest1();
          //遍历学生数组
           test.print(stus);
          System.out.println("*******************");
          //问题一:打印三年级(state值为3)的学生信息。
           test.searchState(stus, 3);
          System.out.println("*********************");
          //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
           test.sort(stus);
          //遍历学生数组
           test.print(stus);
      }
      /**
       * 
       * @Description 遍历Student1[]数组的操作
       * @author xiong
       * @date 2022年3月25日上午11:18:22
       * @param stus
       */
      public void print(Student1[] stus) {
          for(int i = 0;i < stus.length;i++) {
               System.out.println(stus[i].info());
          }
      }
      /**
       * 
       * @Description   查找Student数组中指定年级的学生信息
       * @author xiong
       * @date 2022年3月25日上午11:12:13
       * @param stus   要查找的数组
       * @param state  要查找的年级
       */
      public void searchState(Student1[] stus,int state) {
          for(int i = 0;i < stus.length;i++) {
              if(stus[i].state == state) {
                  System.out.println(stus[i].info());
              } 
          }
      }
      /**
       * 
       * @Description 给Student1数组排序
       * @author xiong
       * @date 2022年3月25日上午11:15:14
       * @param stus
       */
      public void sort(Student1[] stus) {
          for(int i = 0;i < stus.length - 1;i++) {
              for(int j = 0;j < stus.length - 1 - i;j++) {
                  if(stus[j].score > stus[j + 1].score) {
                      Student1 temp = stus[j];
                      stus[j] = stus[j + 1];
                      stus[j + 1] = temp;
                  }
              }
          }
          
      }
}
class Student1{
    int number;
    int state;
    int score;
    //显示学生信息的方法
    public String info
  • 写回答

4条回答 默认 最新

  • 关注

    意思是创建一个 ExerTest1类的对象test
    new用来实例化对象,创建内存,放在主函数里

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 创建了问题 3月27日

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路