0
点赞
收藏
分享

微信扫一扫

2022-3-29作业

绣文字 2022-03-30 阅读 32
java

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

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

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

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

答:

学生类

public class Stu implements Serializable {
    private String name ;
    private int num;
    private int age ;
    private int score ;

    public Stu() {

    }

    public Stu(String name, int num, int age, int score) {
        this.name = name;
        this.num = num;
        this.age = age;
        this.score = score;
    }


    public String getName() {
        return name;
    }

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

    public int getNum() {
        return num;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Stu stu = (Stu) o;
        return num == stu.num && age == stu.age && score == stu.score && Objects.equals(name, stu.name);
    }

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

FileOutputStream

public class zy1 {
    public static void main(String[] args) throws Exception {
    //将数据输入到集合中
    Stu s1 = new Stu("张三" , 123 , 21 ,90);
    Stu s2 = new Stu("李四" , 124 , 22 ,91);
    Stu s3 = new Stu("王二" , 125 , 23 ,92);
    List<Stu> list = new ArrayList<>();
    list.add(s1);
    list.add(s2);
    list.add(s3);
    // 主要用于将一个对象的所有内容整体写入到输出流中。即:把JVM中的对象 保存到本地
    //所谓序列化主要指将一个对象需要存储的相关信息有效组织成字节序列的转化过程
    FileOutputStream fos = new FileOutputStream("s.txt");
    ObjectOutputStream oos = new ObjectOutputStream(fos);

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

    }
}

FileInputStream 

public class zy1in {
    public static void main(String[] args) throws Exception{
//通过ObjectInputStream类  将s.txt文件的对象 读出来
        FileInputStream fis = new FileInputStream("s.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        //读取ois代表的文件中的对象
        Object o = ois.readObject();
        if ( o instanceof List){
            List list = (List) o ;
            System.out.println(list);
        }
        ois.close();
    }
}

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

答:
 继承

public class zyThread extends Thread{
    //继承
    @Override
    public void run() {
        //定义线程具体干什么事
        for (int i = 0; i < 100; i++) {
            //获取当前线程的名字
            System.out.println(this.getName()+":" + i);
        }
    }

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

}

接口

public class zyThread2 implements Runnable{
    //接口
    @Override
    public void run() {
        //定义线程具体干什么事
        for (int i = 0; i < 100; i++) {
            //获取当前线程的名字
            System.out.println( i);
        }
    }
}

匿名内部类

public class zyThread3 {
    //匿名类
    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();
    }
}

测试

public class zyThreadTest {
    public static void main(String[] args) {
        //创建对象并调用strat方法
        MyThread t1 = new MyThread();
        MyThread t2 = new MyThread("t2");
       //启动线程,就是执行run方法

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

 

举报

相关推荐

0 条评论