1.
public class Student implements Serializable { /** * 定义学生类Student ,属性:姓名,学号,年龄,成绩 * 提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode *使用全参构造器创建3个学生对象,放入集合中 *使用对象流将这个学生集合写入到本地 *使用对象流从本地文件把学生信息读出来,并打印 */ private String name; private int id; private int age; private double fraction; public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getFraction() { return fraction; } public void setFraction(double fraction) { this.fraction = fraction; } public Student(String name, int id, int age, double fraction) { this.name = name; this.id = id; this.age = age; this.fraction = fraction; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return id == student.id && age == student.age && Double.compare(student.fraction, fraction) == 0 && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, id, age, fraction); } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", id=" + id + ", age=" + age + ", fraction=" + fraction + '}'; } }
public static void main(String[] args) throws Exception { Student s1 = new Student("嘎子" , 110 , 18 , 90.0); Student s2 = new Student("潘子" , 111 , 19 , 95.5); Student s3 = new Student("华子" , 112 , 20 , 908.5); List<Student> list = new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); FileOutputStream fo = new FileOutputStream("s.student"); ObjectOutputStream oo = new ObjectOutputStream(fo); oo.writeObject(list); oo.close(); }
public static void main(String[] args) throws Exception { FileInputStream fi = new FileInputStream("s.student"); ObjectInputStream oi = new ObjectInputStream(fi); Object o = oi.readObject(); if (o instanceof List){ List list = (List) o; System.out.println(list); } oi.close(); }
2.
public class myThread extends Thread{ public myThread() { } public myThread(String name) { super(name); } @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println(this.getName() + i); } } }
public static void main(String[] args) { myThread t1 = new myThread(); myThread t2 = new myThread("二号线程"); t1.start(); t2.start(); System.out.println("结束"); }
public class myThread2 implements Runnable{ @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println(i); } } }
public static void main(String[] args) { myThread2 mt1 = new myThread2(); myThread2 mt2 = new myThread2(); Thread t1 = new Thread(mt1 , "一号"); Thread t2 = new Thread(mt2 , "二号"); t1.start(); t2.start(); }
public class myThread3Test { public static void main(String[] args) { //匿名内部类 new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println(Thread.currentThread().getName() + i); } } }).start(); } }