3.29作业

/**
 * 1、定义学生类Student ,属性:姓名,学号,年龄,成绩
 * 提供:无参和全参构造器,生成get和set方法,重写toString ,equals ,hashCode
 * <p>
 * 使用全参构造器创建3个学生对象,放入集合中
 * <p>
 * 使用对象流将这个学生集合写入到本地
 * 使用对象流从本地文件把学生信息读出来,并打印
 */
public class Zuoye {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos=new FileOutputStream("x.student");
        ObjectOutputStream oos=new ObjectOutputStream(fos);
        List<Student>list=new ArrayList<>();
        Student s1=new Student("哈哈",1111,22,99);
        Student s2=new Student("嘻嘻",2222,23,98);
        Student s3=new Student("只在",3333,24,97);
        Student s4=new Student("汪汪",4444,25,96);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        oos.writeObject(list);
        FileInputStream fis=new FileInputStream("x.student");
        ObjectInputStream ois=new ObjectInputStream(fis);
        Object o=ois.readObject();

            if (o instanceof List) {
                List list1 = (List) o;
                System.out.println(list1);
            }

        ois.close();
        oos.close();
    }

}
/**
 * 因为java中 继承Thread类侯则无法继承其他类
 * 实现Runnable接口的方式虽然复杂,但不影响该类继承或实现其他接口
 */
public class Zuoye01 extends Thread{
    @Override
    public void run() {
        System.out.println("继承类实现线程");
        for (int i = 0; i < 10; i++) {
            System.out.println(this.getName()+":"+i);
        }
    }

    public Zuoye01() {
    }

    public Zuoye01(String name) {
        super(name);
    }
}
public static void main(String[] args) {
    Zuoye01 t1=new Zuoye01("哈哈");
    Zuoye01 t2=new Zuoye01("xx");
    t1.start();
    t2.start();

}
//接口类
public class Zuoye02 implements Runnable{
    @Override
    public void run() {
        System.out.println("接口类实现线程");
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}


public static void main(String[] args) {
/*    Zuoye01 t1=new Zuoye01("哈哈");
    Zuoye01 t2=new Zuoye01("xx");
    t1.start();
    t2.start();*/
Zuoye02 t4=new Zuoye02();
Zuoye02 t5=new Zuoye02();
Thread xx=new Thread(t4,"hh");
Thread yy=new Thread(t5,"ww");
xx.start();
yy.start();
}
//匿名内部类

new Thread(new Runnable() {
    @Override
    public void run() {
        for (int i = 0; i < 20; 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、付费专栏及课程。

余额充值