【无标题】

1、定义学生类Student ,属性:姓名,学号,年龄,成绩
提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode

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

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

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

import java.util.Comparator;
import java.util.Objects;


class Student<undefined> implements Comparable {

           

            private String name;

            private String sex;

            private int age;

            private double score;

            public Student( String name, String sex, int age, double score) {

                

                this.name = name;

                this.sex = sex;

                this.age = age;

                this.score = score;

            }

            public Student( String name, String sex, double score) {

         

                this.name = name;

                this.sex = sex;

                this.score = score;

            }



            public String getName() {

                return name;

            }

            public void setName(String name) {

                this.name = name;

            }

            public String getSex() {

                return sex;

            }

            public void setSex(String sex) {

                this.sex = sex;

            }

            public int getAge() {

                return age;

            }

            public void setAge(int age) {

                this.age= age;

            }

            public double getScore() {

                return score;

            }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student<?> student = (Student<?>) o;
        age== student.age && Double.compare(student.score, score) == 0
                && Objects.equals(name, student.name) && Objects.equals(sex, student.sex);
    }

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

    public void setScore(double score) {

                this.score = score;

            }


            @Override

            public String toString() {

                return "Student"{

                        ", name='" + name + '\'' +

                        ", sex='" + sex + '\'' +

                        ", age=" + age +

                        ", score=" + score +

                        '}';

            }



      
    class TestStudent {

        public static void main(String[] args) throws IOException, ClassNotFoundException {

            Student s1 = new Student("张三", "男", 18, 90);

            Student s2 = new Student("李四", "男", 18, 93);

            Student s3 = new Student("王五", "男", 18, 94);

            Student s4 = new Student("赵六", "男", 18, 95);

            FileOutputStream fos=new FileOutputStream("1.txt");

            ObjectOutputStream os=new ObjectOutputStream(fos);

            List studentList=new ArrayList<>();

            studentList.add(s1);

            studentList.add(s2);

            studentList.add(s3);

            studentList.add(s4);
            Collections.sort(studentList);
            os.writeObject(studentList);
            os.close();
            FileInputStream fis=new FileInputStream("1.txt");
            ObjectInputStream is = new ObjectInputStream(fis);
            Object O = is.readObject();
            if (O instanceof List) {
                List studentList1 = (List) O;
                System.out.println(studentList1);
            }
            is.close();

        }

    }

2、分别使用继承,实现接口,匿名内部类的方式创建线程,并启动
自己定义线程的run方法,并体会线程的执行过程

class ExtendsThread extends Thread {
    @Override
    public void run() {
        int i = 0;
        while (i <= 100) {
            System.out.println(getName() + "|" + i);
            i++;
        }
    }
}
class ImplementsRunnable implements Runnable {
    @Override
    public void run() {
        int i = 100;
        while (i <= 200) {
            System.out.println(Thread.currentThread().getName() + "||" + i);
            i++;
        }
    }
}
public class zy2 {
    public static void main(String[] args) {
        ExtendsThread  e = new ExtendsThread();
        ImplementsRunnable irt1 = new ImplementsRunnable();
        Thread t = new Thread(irt1);
        e.start();
        t.start();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 100;
                while (i <= 200) {
                    System.out.println(Thread.currentThread().getName() + "|||" + i);
                    i++;
                }
            }
        });
        t1.start();
    }
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值