1、定义学生类Student ,属性:姓名,学号,年龄,成绩
提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode
使用全参构造器创建3个学生对象,放入集合中
使用对象流将这个学生集合写入到本地
使用对象流从本地文件把学生信息读出来,并打印
package com.qiku.day22;
import java.io.Serializable;
import java.util.Objects;
public class Student implements Serializable {
private String name;
private String id;
private int age;
private double score;
public Student() {
}
public Student(String name, String id, int age, double score) {
this.name = name;
this.id = id;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id='" + id + '\'' +
", 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 age == student.age && Double.compare(student.score, score) == 0 && Objects.equals(name, student.name) && Objects.equals(id, student.id);
}
@Override
public int hashCode() {return Objects.hash(name, id, age, score);}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
public double getScore() {return score;}
public void setScore(double score) {this.score = score;}
}
package com.qiku.day22;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Zuo01 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student stu1 = new Student("一号", "01", 18, 80);
Student stu2 = new Student("二号", "02", 19, 90);
Student stu3 = new Student("三号", "03", 20, 100);
List<Student> list1 = new ArrayList<>();
list1.add(stu1);
list1.add(stu2);
list1.add(stu3);
FileOutputStream fos = new FileOutputStream("student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list1);
oos.close();
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
List<Student> list2 ;
if (o instanceof List) {
list2 = (List)o;
for (Student stu : list2) {
System.out.println(stu);
}
}
}
}
2、分别使用继承,实现接口,匿名内部类的方式创建线程,并启动
自己定义线程的run方法,并体会线程的执行过程
package com.qiku.day22;
public class Zuo02 {
public static class JC extends Thread {
public JC() {
}
public JC(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.getName() + ":" + i);
}
}
}
}
class Test01 {
public static void main(String[] args) {
Zuo02.JC a = new Zuo02.JC();
Zuo02.JC b = new Zuo02.JC();
a.start();
b.start();
}
}
public class Zuo02 {
public static class JK implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
class Test02{
public static void main(String[] args) {
Zuo02.JK m1 = new Zuo02.JK();
Thread a = new Thread(m1,"a");
Zuo02.JK m2 = new Zuo02.JK();
Thread b = new Thread(m2,"b");
a.start();
b.start();
}
}
package com.qiku.day22;
public class Zuoy02 {
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().getName()+":"+i);
}
}
}).start();
}
}
1127

被折叠的 条评论
为什么被折叠?



