
急求Java课程作业······
设计一个类,用于表示学生的学号,姓名,班级,所学的物理、数学成绩,并串行化该类,创建应用程序来存储和还原该类的串行化对象。...
设计一个类,用于表示学生的学号,姓名,班级,所学的物理、数学成绩,并串行化该类,创建应用程序来存储和还原该类的串行化对象。
展开
2个回答
展开全部
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Test {
public static void main(String[] args) throws Exception {
Student s1 = new Student("001", "jack", 1, 88.6, 87.5);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"c:\\stu.file"));
oos.writeObject(s1);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"c:\\stu.file"));
Student stu = (Student) ois.readObject();
System.out.println(stu.getStuNo());
System.out.println(stu.getName());
System.out.println(stu.getGrade());
System.out.println(stu.getPhysicalScore());
System.out.println(stu.getMathScore());
}
}
class Student implements Serializable{
private String stuNo;
private String name;
private int grade;
private double physicalScore;
private double mathScore;
public Student() {
}
public Student(String stuNo, String name, int grade, double physicalScore,
double mathScore) {
this.stuNo = stuNo;
this.name = name;
this.grade = grade;
this.physicalScore = physicalScore;
this.mathScore = mathScore;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public double getPhysicalScore() {
return physicalScore;
}
public void setPhysicalScore(double physicalScore) {
this.physicalScore = physicalScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
}
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Test {
public static void main(String[] args) throws Exception {
Student s1 = new Student("001", "jack", 1, 88.6, 87.5);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"c:\\stu.file"));
oos.writeObject(s1);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
"c:\\stu.file"));
Student stu = (Student) ois.readObject();
System.out.println(stu.getStuNo());
System.out.println(stu.getName());
System.out.println(stu.getGrade());
System.out.println(stu.getPhysicalScore());
System.out.println(stu.getMathScore());
}
}
class Student implements Serializable{
private String stuNo;
private String name;
private int grade;
private double physicalScore;
private double mathScore;
public Student() {
}
public Student(String stuNo, String name, int grade, double physicalScore,
double mathScore) {
this.stuNo = stuNo;
this.name = name;
this.grade = grade;
this.physicalScore = physicalScore;
this.mathScore = mathScore;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public double getPhysicalScore() {
return physicalScore;
}
public void setPhysicalScore(double physicalScore) {
this.physicalScore = physicalScore;
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
}
追问
有没有简单一点的啊,我们还没学那么难的
追答
这就是最简单的,根本不难。
别看代码好像很多似的,下面那个Student类是封装类,封装字段的,getter setter方法由IDE自动生成
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询