3.29作业

1.

public class Student implements Serializable {
    /**
     * 定义学生类Student ,属性:姓名,学号,年龄,成绩
     * 提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode
     *使用全参构造器创建3个学生对象,放入集合中
     *使用对象流将这个学生集合写入到本地
     *使用对象流从本地文件把学生信息读出来,并打印
     */
    private String name;
    private int id;
    private int age;
    private double fraction;

    public Student() {

    }

    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 double getFraction() {
        return fraction;
    }

    public void setFraction(double fraction) {
        this.fraction = fraction;
    }

    public Student(String name, int id, int age, double fraction) {
        this.name = name;
        this.id = id;
        this.age = age;
        this.fraction = fraction;


    }

    @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 && Double.compare(student.fraction, fraction) == 0 && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, id, age, fraction);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                ", fraction=" + fraction +
                '}';
    }
}
public static void main(String[] args) throws Exception {
    Student s1 = new Student("嘎子" , 110 , 18 , 90.0);
    Student s2 = new Student("潘子" , 111 , 19 , 95.5);
    Student s3 = new Student("华子" , 112 , 20 , 908.5);
    List<Student> list = new ArrayList<>();
    list.add(s1);
    list.add(s2);
    list.add(s3);
    FileOutputStream fo = new FileOutputStream("s.student");
    ObjectOutputStream oo = new ObjectOutputStream(fo);
    oo.writeObject(list);
    oo.close();


}
public static void main(String[] args) throws Exception {
    FileInputStream fi = new FileInputStream("s.student");
    ObjectInputStream oi = new ObjectInputStream(fi);
    Object o = oi.readObject();
    if (o instanceof List){
        List list = (List) o;
        System.out.println(list);
    }
    oi.close();
}

2.

public class myThread extends Thread{
    public myThread() {
    }

    public myThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(this.getName() + i);
        }

    }
}
public static void main(String[] args) {
    myThread t1 = new myThread();
    myThread t2 = new myThread("二号线程");
    t1.start();
    t2.start();
    System.out.println("结束");
}

public class myThread2 implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(i);
        }
    }
}
public static void main(String[] args) {
    myThread2 mt1 = new myThread2();
    myThread2 mt2 = new myThread2();
    Thread t1 = new Thread(mt1 , "一号");
    Thread t2 = new Thread(mt2 , "二号");
    t1.start();
    t2.start();
}
public class myThread3Test {
    public static void main(String[] args) {
        //匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName() + i);
                }
            }
        }).start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值