package com.qiku.yrc.work22; import java.io.Serializable; import java.util.Objects; /** * 1、定义学生类Student ,属性:姓名,学号,年龄,成绩 * 提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode * * 使用全参构造器创建3个学生对象,放入集合中 * * 使用对象流将这个学生集合写入到本地 * * 使用对象流从本地文件把学生信息读出来,并打印 */ public class Student implements Serializable { private String name; private int studentNum; private int age; private int score; public Student() { } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", studentNum=" + studentNum + ", age=" + age + ", score=" + score + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return studentNum == student.studentNum && age == student.age && score == student.score && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, studentNum, age, score); } public Student(String name, int studentNum, int age, int score) { this.name = name; this.studentNum = studentNum; this.age = age; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStudentNum() { return studentNum; } public void setStudentNum(int studentNum) { this.studentNum = studentNum; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
package com.qiku.yrc.work22; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; public class StudentOutPutStreamTest { public static void main(String[] args) throws Exception { Student s1=new Student("张三",2222,19,88); Student s2=new Student("张四",2223,12,89); Student s3=new Student("杨六",2228,39,98); List<Student> list = new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); System.out.println(list); FileOutputStream fos = new FileOutputStream("student.list"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(list); oos.close(); } }
package com.qiku.yrc.work22; import java.io.FileInputStream; import java.io.ObjectInputStream; import java.util.List; public class StudentInputStreamTest { public static void main(String[] args) throws Exception { FileInputStream fis= new FileInputStream("student.list"); ObjectInputStream ois=new ObjectInputStream(fis); Object o = ois.readObject(); if (o instanceof List){ List list= (List) o; System.out.println(list); } ois.close(); } }
package com.qiku.yrc.work22; public class MyThread extends Thread{ public MyThread() { } public MyThread(String name) { super(name); } /** * 2、分别使用继承,实现接口,匿名内部类的方式创建线程,并启动 * 自己定义线程的run方法,并体会线程的执行过程 */ @Override public void run() { super.run(); for (int i = 0; i < 66; i++) { System.out.println(this.getName()+":"+i); } } } class MyThreadTest{ public MyThreadTest() { } public static void main(String[] args) { MyThread mt=new MyThread();//若都是 有参构造器 则start仅执行一次?????? mt.start(); MyThread mt2=new MyThread("md2"); mt2.start(); //创建该类的对象作为实参来构造 Thread类型的 对象 //然后使用 Thread 调用start方法 MyThreadTest mt01 = new MyThreadTest(); } }
package com.qiku.yrc.work22; public class ThreadW2 implements Runnable{ @Override public void run() { for (int i = 0; i < 78; i++) { System.out.println(i); } } //自定义类 实现 Runnable 接口 重写run方法 } class ThreadW2Test{ public static void main(String[] args) { //创建该类的对象作为实参来构造 Thread类型的 对象 //然后使用 Thread 调用start方法 ThreadW2 t1 = new ThreadW2(); Thread t11= new Thread(t1,"lalal"); ThreadW2 t2 = new ThreadW2(); Thread t22= new Thread(t2,"lalalda "); t11.start(); t22.start(); System.out.println(t11.getName()); System.out.println(t22.getName()); }}
package com.qiku.yrc.day22; public class ThreadW3 { public static void main(String[] args) { //使用匿名内部类的方式来创建和启动线程 new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println(i); Thread.currentThread().getName(); } } }).start(); } }