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

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 14: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月23日
  • 已采纳回答 10月15日
  • 修改了问题 10月15日
  • 创建了问题 10月15日

悬赏问题

  • ¥15 使用小米路由器组建局域网,mac使用ssh无法连接linux主机
  • ¥15 布置的py昨夜,但是不知道咋做
  • ¥15 关于Java的学习问题
  • ¥15 如何使用chatgpt完成文本分类任务?
  • ¥15 已知速度v关于位置s的等式,怎么转化为已知位置求速度v的等式
  • ¥15 我有个餐饮系统,用wampserver把环境配置好了,但是后端的网页却进去,是为什么,能不能帮远程一下?
  • ¥15 R运行没有名称为"species"的插槽对于此对象类"SDMmodelCV"
  • ¥20 基于决策树的数字信号处理,2ask 2psk 2fsk的代码,检查下报错的原因
  • ¥15 wincc已组态的变量过多
  • ¥60 如图:直线与椭圆X轴平行,求直线与椭圆任意一点的相切坐标计算公式