这些类的继承关系有些类似,弄一个作为样例,理解一下其中的机制。
package cc.openhome; import java.io.*; public class Member { private String number; private String name; private int age; public Member(String number, String name, int age) { this.number = number; this.name = name; this.age = age; } @Override public String toString() { return String.format("(%s, %s, %d)", number, name, age); } public void save() { try{ DataOutputStream output = new DataOutputStream(new FileOutputStream(number)); output.writeUTF(number); output.writeUTF(name); output.writeInt(age); } catch(IOException ex) { throw new RuntimeException(ex); } } public static Member load(String number) { Member member = null; try { DataInputStream input = new DataInputStream(new FileInputStream(number)); member = new Member(input.readUTF(), input.readUTF(), input.readInt()); } catch(IOException ex) { throw new RuntimeException(ex); } return member; } }
package cc.openhome; import java.io.*; public class MemberDemo { public static void main(String[] args) { Member[] members = { new Member("B1234", "Justing", 90),new Member("B4532", "Monica", 87)}; for(Member member : members) { member.save(); } System.out.println(Member.load("B1234")); } }
时间: 2024-10-09 21:44:24