会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132441个问题

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器 2431楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2432楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2434楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2436楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2437楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 2438楼

public class Student implements Serializable {
    private String name;
    private int age;
    public static String schoolName;
    private transient String pwd;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", pwd='" + pwd + '\'' +
                '}'+"schoolName="+schoolName;
    }
}
public class Test {
    public static void main(String[] args) {
        write();
        read();
    }

    //写对象方法
    public static void write(){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("d:/a.txt"));
            Student stu = new Student("marry",20,"888888");
            Student.schoolName = "北校区";
            oos.writeObject(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    //读对象方法
    public static void read(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("d:/a.txt"));
            Student stu = (Student) ois.readObject();
            System.out.println(stu);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果:

image.png

老师,为什么我这个静态成员schoolName可以被序列化?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2441楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2443楼

老师,这里Thread01类中for循环的println()里我没加" i ",而是直接打印了线程名字,如下代码:

package com.ljp.Runnable;

public class Thread01 implements Runnable{

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName() + "------------------");
            // System.out.println(Thread.currentThread().getName() + "------------------" + i);
        }
    }
}

在主线程中,println()里面有" i ",如下代码:

package com.ljp.Runnable;

public class Test {
    public static void main(String[] args) {
        Thread01 t01 = new Thread01();
        Thread td01 = new Thread(t01,"北坡的八百标兵");
        td01.start();

        for(int i=0; i<10;i++){
            System.out.println(Thread.currentThread().getName() + "-------------------------------" + i);
        }
    }
}

运行结果,主线程结果总是在线程1运行结果后面,如下运行结果:

北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
北坡的八百标兵------------------
main-------------------------------0
main-------------------------------1
main-------------------------------2
main-------------------------------3
main-------------------------------4
main-------------------------------5
main-------------------------------6
main-------------------------------7
main-------------------------------8
main-------------------------------9

但是Thread01类的println()加上 " i ",就不会出现上面情况,运行结果如下:

main-------------------------------0
北坡的八百标兵------------------0
北坡的八百标兵------------------1
main-------------------------------1
北坡的八百标兵------------------2
main-------------------------------2
北坡的八百标兵------------------3
北坡的八百标兵------------------4
北坡的八百标兵------------------5
main-------------------------------3
北坡的八百标兵------------------6
main-------------------------------4
北坡的八百标兵------------------7
main-------------------------------5
北坡的八百标兵------------------8
main-------------------------------6
北坡的八百标兵------------------9
main-------------------------------7
main-------------------------------8
main-------------------------------9

为什么?????

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2445楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637