1、定义学生类Student ,属性:姓名,学号,年龄,成绩
提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode
使用全参构造器创建3个学生对象,放入集合中
使用对象流将这个学生集合写入到本地
使用对象流从本地文件把学生信息读出来,并打印
public static void main(String[] args) throws Exception{
// Student stu1 = new Student("张三",20,"0001",92);
// Student stu2 = new Student("李四",22,"0002",95.5);
// Student stu3 = new Student("常同学",21,"0003",99);
// List<Student> list = new ArrayList<>();
// list.add(stu1);
// list.add(stu2);
// list.add(stu3);
使用对象流将这个学生集合写入到本地
// FileOutputStream fos = new FileOutputStream("student.txt");
// ObjectOutputStream oos = new ObjectOutputStream(fos);
// oos.writeObject(list);
// oos.close();
// 使用对象流从本地文件把学生信息读出来,并打印
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if (o instanceof List){
List list1 = (List)o;
for (Object l : list1){
System.out.println(l);
}
}
ois.close();
}
class Student implements Serializable{
private String name;
private int age;
private String ID;
private double score;
public Student() {
}
public Student(String name, int age, String ID, double score) {
this.name = name;
this.age = age;
this.ID = ID;
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", ID='" + ID + '\'' +
", 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, age, ID, score);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
2、分别使用继承,实现接口,匿名内部类的方式创建线程,并启动 自己定义线程的run方法
继承
public class MyThread01 extends Thread {
public MyThread01() {
}
public MyThread01(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) {
MyThread01 m1 = new MyThread01();
MyThread01 m2 = new MyThread01("m2");
m1.start();
m2.start();
}
}
接口
public class MyThread02 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
class Test02{
public static void main(String[] args) {
MyThread02 m1 = new MyThread02();
Thread t1 = new Thread(m1,"t1");
MyThread02 m2 = new MyThread02();
Thread t2 = new Thread(m2,"t2");
t1.start();
t2.start();
}
}
匿名内部类
public class MyThread {
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();
}
}