// 1、定义学生类Student ,属性:姓名,学号,年龄,成绩
// 提供:无参和全参构造器,生成get和set方法,重写toString ,equals,hashCode
// 使用全参构造器创建3个学生对象,放入集合中
// 使用对象流将这个学生集合写入到本地
// 使用对象流从本地文件把学生信息读出来,并打印
public static void main(String[] args) throws Exception {
Student t1 = new Student("李梦",2017,18,86);
Student t2 = new Student("张三",2018,19,90);
Student t3 = new Student("刘张",2020,20,95);
List<Student> list = new ArrayList<>();
list.add(t1);
list.add(t2);
list.add(t3);
FileOutputStream fis = new FileOutputStream("E:\\12\\a.txt");
ObjectOutputStream oos = new ObjectOutputStream(fis);
FileInputStream fm = new FileInputStream("E:\\12\\a.txt");
ObjectInputStream obj = new ObjectInputStream(fm);
oos.writeObject(t1);
oos.writeObject(t2);
oos.writeObject(t3);
obj.readObject();
oos.close();
obj.close();
}
}
class Student implements Serializable{
private String name;
private int ID;
private int age;
private int Cj;
public Student() {
}
public Student(String name, int ID, int age, int cj) {
this.name = name;
this.ID = ID;
this.age = age;
Cj = cj;
}
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 getCj() {
return Cj;
}
public void setCj(int cj) {
Cj = cj;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", ID=" + ID +
", age=" + age +
", Cj=" + Cj +
'}';
}
@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 && Cj == student.Cj && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, ID, age, Cj);
}
}
// 2、分别使用继承,实现接口,匿名内部类的方式创建线程,并启动
// 自己定义线程的run方法,并体会线程的执行过程
public class zuoye {
public static void main(String[] args) {
Test t1 = new Test("1号线程");
Test t2 = new Test("2号线程");
t1.start();
t2.start();
}
}
class Test extends Thread{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(this.getName()+":"+i);
}
}
public Test() {
}
public Test(String name) {
super(name);
}
}
//接口类型
public class zuoye {
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
Thread t11 = new Thread(t1, "t1");
Thread t22 = new Thread(t2,"t2");
t11.start();
t22.start();
}
}
class Test implements Runnable{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
}
public Test() {
}
}
//匿名内部类
public class zuoye {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(
Thread.currentThread().getName()+i
);
}
}
},"t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
}
},"t2");
t1.start();
t2.start();
}
}