1、定义学生类Student ,属性:姓名,学号,年龄,成绩
提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode
使用全参构造器创建3个学生对象,放入集合中
使用对象流将这个学生集合写入到本地
使用对象流从本地文件把学生信息读出来,并打印
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Demo01 {
public static void main(String[] args) throws Exception {
student stu1 = new student("张三", 123, 12, 99);
student stu2 = new student("李四", 124, 11, 90);
student stu3 = new student("赵武", 125, 15, 100);
List<student> list = new ArrayList<>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
FileOutputStream fos = new FileOutputStream("444.text");
BufferedOutputStream ops = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(ops);
FileInputStream fis = new FileInputStream("444-1.text");
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
oos.writeObject(list);
ois.readObject();
oos.close();
ois.close();
}
}
class student implements Serializable{
private String name;
private int ID;
private int age;
private int score;
public student(String name, int ID, int age, int score) {
this.name = name;
this.ID = ID;
this.age = age;
this.score = score;
}
public student() {
}
@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 && score == student.score && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, ID, age, score);
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", ID=" + ID +
", age=" + age +
", score=" + score +
'}';
}
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 int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
2、分别使用继承,实现接口,匿名内部类的方式创建线程,并启动
自己定义线程的run方法,并体会线程的执行过程
public class Demo02 {
public static void main(String[] args) {
// myThread mt = new myThread();
// myThread mt2 = new myThread();
// Thread t = new Thread(mt,"张三");
// Thread t2 = new Thread(mt2,"李四");
// t.start();
// t2.start();
}
}
//class myThread extends Thread{
// public void run(){
// for (int i = 0; i < 123; i++) {
// System.out.println(Thread.currentThread().getName()+":"+i);
// }
// }
//}
//class myThread implements Runnable{
//
// @Override
// public void run() {
// for (int i = 0; i < 122; i++) {
// System.out.println(Thread.currentThread().getName()+":"+i);
// }
// }
//}