日期:2014-05-20 浏览次数:20698 次
class Student { private Integer stuId;// 學號 private String name;// 姓名 private Boolean gender;// 性別 private String address;// 地址 private String gradeNo;// 級 private String classNo;// 班 // 無參構造函數 public Student() { } // 全參構造函數 public Student(Integer stuId, String name, Boolean gender, String address, String gradeNo, String classNo) { this.stuId = stuId; this.name = name; this.gender = gender; this.address = address; this.gradeNo = gradeNo; this.classNo = classNo; } public Integer getStuId() { return stuId; } public void setStuId(Integer stuId) { this.stuId = stuId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getGender() { return gender; } public void setGender(Boolean gender) { this.gender = gender; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getGradeNo() { return gradeNo; } public void setGradeNo(String gradeNo) { this.gradeNo = gradeNo; } public String getClassNo() { return classNo; } public void setClassNo(String classNo) { this.classNo = classNo; } @Override public String toString() { return "{stuId:" + this.stuId + ",name:" + this.name + "}"; } }
------解决方案--------------------
public class Student { private int studentNumber;//学号 private int studentClass;//班级 private char sex;//性别 private int age;//年龄 public int getAge() {//该方法获取年龄 return age; } public void setAge(int age) {//该方法设置年龄 this.age = age; } public char getSex() {//该方法获取性别 return sex; } public void setSex(char sex) {//该方法设置性别 this.sex = sex; } public int getStudentClass() {//该方法获取班级 return studentClass; } public void setStudentClass(int studentClass) {//该方法设置班级 this.studentClass = studentClass; } public int getStudentNumber() {//该方法获取学号 return studentNumber; } public void setStudentNumber(int studentNumber) {//该方法设置学号 this.studentNumber = studentNumber; } public Student() { } public Student(int studentNumber, int studentClass, char sex, int age) { this.studentNumber = studentNumber; this.studentClass = studentClass; this.sex = sex; this.age = age; } @Override public String toString() { return "学号:" + studentNumber + ",班级:" + studentClass + ",性别:" + sex + ",年龄" + age; } public static void main(String[] args) { Student s1 = new Student(1, 3, '男', 21); System.out.println(s1); s1.setAge(22); System.out.println(s1); } }
------解决方案--------------------
保存到文件吧 再从文件中读取数据 并输出
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; public class Student implements Serializable{ private int studentNumber;//学号 private int studentClass;//班级 private char sex;//性别 private int age;//年龄 public int getAge() {//该方法获取年龄 return age; } public void setAge(int age) {//该方法设置年龄 this.age = age; } public char getSex() {//该方法获取性别 return sex; } public void setSex(char sex) {//该方法设置性别 this.sex = sex; } public int getStudentClass() {//该方法获取班级 return studentClass; } public void setStudentClass(int studentClass) {//该方法设置班级 this.studentClass = studentClass; } public int getStudentNumber() {//该方法获取学号 return studentNumber; } public void setStudentNumber(int studentNumber) {//该方法设置学号 this.studentNumber = studentNumber; } public Student() { } public Student(int studentNumber, int studentClass, char sex, int age) { this.studentNumber = studentNumber; this.studentClass = studentClass; this.sex = sex; this.age = age; } @Override public String toString() { return "学号:" + studentNumber + ",班级:" + studentClass + ",性别:" + sex + ",年龄" + age; } public static void main(String[] args) throws Exception{ ArrayList<Student> students = new ArrayList<Student>(); students.add(new Student(1, 1, '男', 21)); students.add(new Student(2, 1, '男', 21)); students.add(new Student(3, 1, '男', 21)); students.add(new Student(4, 1, '男', 21)); students.add(new Student(5, 1, '男', 21)); students.add(new Student(6, 1, '女', 21)); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( "c:/student.txt")); oos.writeObject(students); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream( "c:/student.txt")); students = (ArrayList<Student>) ois.readObject(); for (int i = 0; i < students.size(); i++) { System.out.println("*********************第" + i + "个学生**************************"); System.out.println(students.get(i)); } } } /* *********************第0个学生************************** 学号:1,班级:1,性别:男,年龄21 *********************第1个学生************************** 学号:2,班级:1,性别:男,年龄21 *********************第2个学生************************** 学号:3,班级:1,性别:男,年龄21 *********************第3个学生************************** 学号:4,班级:1,性别:男,年龄21 *********************第4个学生************************** 学号:5,班级:1,性别:男,年龄21 *********************第5个学生************************** 学号:6,班级:1,性别:女,年龄21 */