6.static静态--day08_14_静态static的内存图

静态static的内存图

编写student.java类

package com.iflytek.day08.demo03;

public class Student {
    
    

    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;
    }
}

编写Demo03StaticStudent.java类

package com.iflytek.day08.demo03;

public class Demo03StaticStudent {
    
    

    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("黄蓉", 18);
        System.out.println("two的姓名:" + two.getName());
        System.out.println("two的年龄:" + two.getAge());
        System.out.println("two的教室:" + Student.room);
    }

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lucky_shi/article/details/113090966