1、定义学生类Student ,属性:姓名,学号,年龄,成绩
提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode
使用全参构造器创建3个学生对象,放入集合中
使用对象流将这个学生集合写入到本地
使用对象流从本地文件把学生信息读出来,并打印
package com.qiku.day22; import javax.sql.rowset.serial.SerialArray; import java.io.Serializable; import java.util.Objects; public class Student implements Serializable { private String name; private int xh; private double score; public int age; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return xh == student.xh && Double.compare(student.score, score) == 0 && age == student.age && Objects.equals(name, student.name); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getXh() { return xh; } public void setXh(int xh) { this.xh = xh; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", xh=" + xh + ", score=" + score + ", age=" + age + '}'; } @Override public int hashCode() { return Objects.hash(name, xh, score, age); } public Student(String name, int xh, double score, int age) { this.name = name; this.xh = xh; this.score = score; this.age = age; } public Student() { } }
package com.qiku.day22; import java.io.*; import java.util.ArrayList; public class ZY01 { public static void main(String[] args) throws IOException { Student s1=new Student("啊啊",23,34,45); Student s2=new Student("十多个",23,56,25); Student s3=new Student("安抚",43,64,15); Student s4=new Student("食发鬼",33,44,85); ArrayList<Object> arr = new ArrayList<>(); arr.add(s1); arr.add(s2); arr.add(s3); arr.add(s4); FileOutputStream fis = new FileOutputStream("s.list"); ObjectOutputStream oos = new ObjectOutputStream(fis); oos.writeObject(arr); oos.close(); } }
2、分别使用继承,实现接口,匿名内部类的方式创建线程,并启动
自己定义线程的run方法,并体会线程的执行过程
package com.qiku.day22; //自定义类 继承Thread类 并重写run方法 public class MyThread extends Thread{ //定义该线程具体干什么事 @Override public void run(){ System.out.println("MyThread开始"); for (int i = 0; i < 100; i++) { System.out.println(this.getName()+":"+i); } } public MyThread(String name) { super(name); } public MyThread() { } }
package com.qiku.day22; public class MyThreadTest { public static void main(String[] args) { //然后创建该类对象调用start方法 MyThread myThread=new MyThread(); MyThread myThread1=new MyThread("阿萨"); //启动线程 就是执行run方法 myThread.start(); myThread1.start(); System.out.println("先执行我"); } }
匿名内部类的方式
package com.qiku.day22; public class MyThreadTest3 { public static void main(String[] args) { //使用匿名内部类的方式来创建和启动线程 new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println( // Thread.currentThread()获取正在运行的线程的对象 Thread.currentThread().getName()+i ); } } }).start(); } }