静态static的内存图

    private String name;
    private int age;
    static String room;

    public Student() {
    }

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

    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 static String getRoom() {
        return room;
    }

    public static void setRoom(String room) {
        Student.room = room;
    }
    public static void main(String[] args) {
        Student.room = "101教室";
        Student one = new Student("张三",20);
        System.out.println("one的姓名:"+one.getName());
        System.out.println("one的年龄:"+one.getAge());
        System.out.println("one的教室:"+Student.room);
        System.out.println("================");
        Student two = new Student("李四",22);
        System.out.println("two的姓名:"+two.getName());
        System.out.println("two的年龄:"+two.getAge());
        System.out.println("two的教室:"+Student.room);
    }

注意: 根据类名称访问静态成员变量的时候,全程和对象就没关系,只和类有关系。

 

 

 

 

 

posted @ 2022-06-30 14:46  魔光领域  阅读(18)  评论(0编辑  收藏  举报