把对象变为文件&多线程的实现

把对象变为文件 & 多线程的实现

  1. 定义学生类Student ,属性:姓名,学号,年龄,成绩

提供:无参和全参构造器,生成get和set方法,重写toStringequals ,hashCode

使用全参构造器创建3个学生对象,放入集合中

使用对象流将这个学生集合写入到本地

使用对象流从本地文件把学生信息读出来,并打印

//Studen类

import java.io.Serializable;
import java.util.Objects;

public class Student implements Serializable {
    private String name;
    private String num;
    private int age;
    private int cscore;

    public Student(String name, String num, int age, int cscore) {
        this.name = name;
        this.num = num;
        this.age = age;
        this.cscore = cscore;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getCscore() {
        return cscore;
    }

    public void setCscore(int cscore) {
        this.cscore = cscore;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", num='" + num + '\'' +
                ", age=" + age +
                ", cscore=" + cscore +
                '}';
    }

    @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 && cscore == student.cscore && Objects.equals(name, student.name) && Objects.equals(num, student.num);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, num, age, cscore);
    }
}
//创建对象 写入文件
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class StudentTest {
    public static void main(String[] args) throws Exception {

        Student s1 = new Student("张一","10001",18,90);
        Student s2 = new Student("张二","10002",19,89);
        Student s3 = new Student("张三","10003",20,88);

        List<Student> list =new ArrayList<>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
        FileOutputStream fos = new FileOutputStream("student.list");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(list);
        oos.close();

    }
}
//读取文件 输出内容
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class StudentTestRead {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("student.list");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object o = ois.readObject();

        System.out.println(o);
    }
}
  1. 承,实现接口,匿名内部类的方式创建线程,并启动自己定义线程的run方法,并体会线程的执行过程
//创建一个类继承 Thread
public class Study  extends Thread{
    public Study(String name) {
        super(name);
    }
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("学习"+this.getName()+i+"次。");
        }
    }
}
//以继承的方式实现多线程
public class ThreadDemo01 {
    public static void main(String[] args) {
        Study s1 = new Study("语文");
        Study s2 = new Study("数学");
        s1.start();
        s2.start();
    }
}
//定义Study2 实现 Runnable
public class Study2  implements Runnable{
private String name;

     public Study2(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("学习"+this.getName()+i+"次。");
        }
    }
}
//用实现接口的方式实现多线程
public class ThreadDemo02 {
    public static void main(String[] args) {
        Study2 s1 =new Study2("英语");
        Study2 s2 =new Study2("物理");

        Thread t1 = new Thread(s1);
        Thread t2 = new Thread(s2);

        t1.start();
        t2.start();

    }
}
//用内部类的方式实现多线程
public class ThreadDemo03 {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    System.out.println("我吃了"+i+"次");
                }
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 30; i++) {
                    System.out.println("我家的狗狗生"+i+"只");
                }
            }
        }).start();
        t1.start();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值