代码如下:
import java.io.*;
public class ObjectSerializationExample1{
   
    
     class Student implements Serializable{
     private String name;
     private int chScore,engScore,mathScore;

     public Student(String n,int c,int e,int m){
   name=n;
   chScore=c;
   engScore=e;
   mathScore=m;
                   }


   public double avgScore(){
   return(chScore+engScore+mathScore)/3.0;}

   public void printData(){
  System.out.println("name:"+name);
  System.out.println("chinese:"+chScore);
  System.out.println("english:"+engScore);
  System.out.println("mant:"+mathScore);
      System.out.println("average:"+avgScore());
     }
     }
    
    public static void main(String arg[]){
     int c=Integer.parseInt(arg[1]);
     int e=Integer.parseInt(arg[2]);
     int m=Integer.parseInt(arg[3]);
     
     
     Student st=new Student(arg[0], c, e,m);//提示出错处.???????
     st.printData();
     
     try{
       FileOutputStream  fout=new FileOutputStream("Data.txt");
       ObjectOutputStream  objOut=new ObjectOutputStream(fout);
       
       objOut.writeObject(st);
       objOut.close();
       fout.close();
      }
      
      catch(IOException ie){
       System.err.println(ie);
      }
      
    }
  }提示出错为:
ObjectSerializationExample1.java:37: non-static variable this cannot be referenced from a static context
     Student st=new Student(arg[0], c, e,m);
                ^
1 error

解决方案 »

  1.   

    I'm here.
    非静态内类是不能这么创建对象的,这样可以
    ObjectSerializationExample1 ose = new ObjectSerializationExample1();
    Student st = ose.new Student(arg[0],c,e,m);
      

  2.   

    樓主,你可以把 class Student implements Serializable
    改為 static class Student implements Serializable //靜態類 就OK了
      

  3.   

    package LastMonth;import java.io.*;
    class Student implements Serializable{
        private String name;
        private int chScore,engScore,mathScore;    public Student(String n,int c,int e,int m){
       name=n;
       chScore=c;
       engScore=e;
       mathScore=m;
                  }
      public double avgScore(){
      return(chScore+engScore+mathScore)/3.0;}  public void printData(){
      System.out.println("name:"+name);
      System.out.println("chinese:"+chScore);
      System.out.println("english:"+engScore);
      System.out.println("mant:"+mathScore);
     System.out.println("average:"+avgScore());
        }
    }public class ObjectSerializationExample1{
        
        public static void main(String arg[]){
         int c=Integer.parseInt(arg[1]);
         int e=Integer.parseInt(arg[2]);
         int m=Integer.parseInt(arg[3]);
         
         
         Student st = new Student(arg[0], c, e,m);//提示出错处.???????
         st.printData();
         
         try{
           FileOutputStream  fout=new FileOutputStream("Data.txt");
           ObjectOutputStream  objOut=new ObjectOutputStream(fout);
           
           objOut.writeObject(st);
           objOut.close();
           fout.close();
          }
          
          catch(IOException ie){
           System.err.println(ie);
          }
          
        }
      }