mr_nooko 2021-10-15 03:47 采纳率: 60%
浏览 37
已结题

java类成员问题,引用类成员时报错

代码的目的是为了通过输入五组学生数据,进行储存并且遍历显示,一下是源代码
分别在第二个和第三个类文件中调用Student类成员时报错,显示缺少方法

//第一个类文件
package StudentInfor;
import java.io.*;
public class Student implements Serializable {
    private String id;    //学号
    private String name;    //姓名
    private int eng;    //英语成绩
    private int math; //数学成绩
    private int comp; //计算机成绩
    private int sum;  //总成绩
    public Student(String id,String name, int eng, int math,int comp){//生成构造函数
        this.id=id;
        this.name=name;
        this.eng=eng;
        this.math=math;
        this.comp=comp;
        sum();   //计算总成绩
    }    
    public static Student getAStudent(int i){
        Student studenti;                    
        System.out.println("输入第" + i + "个学生的信息:"); 
        System.out.print("学号:"); 
        String id = Keyboard.getString(); 
        System.out.print("姓名:"); 
        String name = Keyboard.getString();         
        System.out.print("英语成绩:"); 
        int eng = Keyboard.getInteger();         
        System.out.print("数学成绩:"); 
        int math = Keyboard.getInteger();    
        System.out.print("计算机成绩:"); 
        int comp = Keyboard.getInteger();         
        studenti = new Student(id,name,eng,math,comp);
        return studenti;
    }
    public void setId(String id){
        this.id=id;
    }

    
    public void setName(String name)
    {
        this.name=name;
    }    
    public void setEng(int eng)
    {
        this.eng=eng;
        sum();            //计算总成绩
    }
    public void setMath(int math)
    {
        this.math=math;
        sum();            //计算总成绩
    }
    public void setComp(int comp){
        this.comp=comp;
        sum();            //计算总成绩
    }        
    public String getId(){//读取成员变量并转换为字符串返回
        return id;
    }    
      public String getName(){
        return name;
    }
      public int getEng(){ return eng; }
      public int getMath(){ return math; }
      public int getComp(){ return comp; }
         public int getSum(){ return sum; }
      final void sum(){ 
         sum=eng+math+comp;
       }
     public String toString(){
         return getId() + "\t"+getName() +"\t"+getEng() + "\t"+getMath() +"\t"+getComp() +"\t"+getSum();
      }   
     public boolean equals(Object x) { 
           if (this.getClass() != x.getClass()) return false;      
           Student b = (Student) x;     
           return (this.getId().equals(b.getId())); 
     }    
     //比较成绩大小,当前对象成绩比参数对象成绩大时返回1,相等时返回0,其它返回-1.
     public int compare(Student A){
         if(this.getSum()>A.getSum()) return 1;
         else if(this.getSum()==A.getSum()) return 0;
         else return -1;
     }    




    public static void main(String[] args) {
        // TODO Auto-generated method stub

        
    }
}
//第二个类文件
package StudentInfor;
import java.io.*;

public class StudentClass{
    private String name;             //班级名称
    static int capacity = 40;        //最大容量
    private Student students[];      //学生
    private int size;                //实际人数
    public StudentClass(String name, int size){
        this.name = name;
        this.size = size;
        students = new Student[capacity];
    }     
    public String getName(){ return name; }
     public int getCapacity(){ return capacity; }        
     public Student[] getStudents(){ return students; }
     public int getSize(){ return size; } 
     public void setName(String name){ this.name =name;}
     public void setCapacity(int capacity)
     {
            this.capacity = capacity;
     }        
        public void setSize(int size){
            this.size = size;        
        }
        public void setStudents(Student[] students){
            for (int i = 0; i<size; i++)
               this.students[i] = new Student(students[i]);//报错-----------------------------------------------------
        }    
        public String toString(){
            String s;
            s = " 班级:" + name +"\t" + "容量:" + capacity + "\t" + "实际人数:" + size +"\n\n";
            s = s + "学号"+"\t" + "姓名"+"\t" + "英语"+"\t" + "数学" + "\t" + "计算机" +"\t" + "总成绩\n";        for (int i=0; i<size; i++)
            s = s + students[i].getId()+"\t"+  students[i].getName()+ "\t"  +students[i].getEng()+ "\t" +students[i].getMath()+"\t"+
    students[i].getComp()+"\t"+students[i].getSum()+"\n";  
               return s;
        }
    }
//第三个类
package StudentInfor;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Tester1{        
  public static void main(String args[]){
     StudentClass aClass = new StudentClass("信管",5);     Student students[]=new Student[5];        
    for (int i=0; i<5; i++)
           students[i] = new Student(getAStudent(i+1));    //报错------------
     aClass.setStudents(students);        
    System.out.println(aClass);    
    try {            
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream ("D:\\java学习\\java junk\\StudentInfor\\src\\StudentInfor\\student.ser"));
        for (int i=0; i<5; i++)
           oos.writeObject(students[i]);
        oos.close();
    }catch(Exception e){
       System.out.println(e) ;
    }
    }    
  public static Student getAStudent(int i){
        Student studenti;                    
        System.out.println("输入第" + i + "个学生的信息:"); 
        System.out.print("学号:"); 
        String id = Keyboard.getString(); 
        System.out.print("姓名:"); 
        String name = Keyboard.getString();         
        System.out.print("英语成绩:"); 
        int eng = Keyboard.getInteger();         
        System.out.print("数学成绩:"); 
        int math = Keyboard.getInteger();    
        System.out.print("计算机成绩:"); 
        int comp = Keyboard.getInteger();         
        studenti = new Student(id,name,eng,math,comp);
        return studenti;
    }
}
//第四个类
package StudentInfor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Keyboard{
    static BufferedReader inputStream = new BufferedReader (new InputStreamReader(System.in));    
    public static int getInteger(){ 
        try { 
           return (Integer.valueOf(inputStream.readLine().trim()).intValue()); 
         }catch(Exception e){ 
             e.printStackTrace(); 
             return 0; 
         } 
      }
    public static String getString() { 
        try{
           return (inputStream.readLine()); 
         }catch (IOException e){ 
              return "0";
         }  
   }
}

展开全部

  • 写回答

2条回答 默认 最新

  • kse_music 2021-10-15 06:50
    关注
    for (int i=0; i<5; i++){
            students[i] = getAStudent(i+1);    //报错------------
        }
    
    
    public void setStudents(Student[] students){
        for (int i = 0; i<size; i++)
            this.students[i] = students[i];//报错-----------------------------------------------------
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 10月22日
  • 已采纳回答 10月15日
  • 修改了问题 10月15日
  • 创建了问题 10月15日

悬赏问题

  • ¥15 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部