当前位置: 移动技术网 > IT编程>开发语言>Java > Memory map of an object array

Memory map of an object array

2018年10月16日  | 移动技术网IT编程  | 我要评论
student类:
package com.itheima;
/*
 * 自动生成构造方法:
 *         代码区域右键 -- source -- generate constructors from superclass...    无参构造方法
 *         代码区域右键 -- source -- generate constructor using fields...        带参构造方法
 * 自动生成getxxx()/setxxx():
 *         代码区域右键 -- source -- generate getters and setters...
 */
public class student {
    private string name;
    private int age;
    
    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;
    }
    
    
}
student的测试类:
package com.itheima;
/*
 * 创建一个学生数组,存储三个学生对象并遍历
 * 
 * 分析:
 *         a:定义学生类
 *         b:创建学生数组
 *         c:创建学生对象
 *         d:把学生对象作为元素赋值给学生数组
 *         e:遍历学生数组
 */
public class studentdemo {
    public static void main(string[] args) {
        //创建学生数组
        student[] students = new student[3];
        
        //创建学生对象
        student s1 = new student("曹操",40);
        student s2 = new student("刘备",35);
        student s3 = new student("孙权",30);
        
        //把学生对象作为元素赋值给学生数组
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        
        //遍历学生数组
        for(int x=0; x<students.length; x++) {
            student s = students[x];
            //system.out.println(s);
            system.out.println(s.getname()+"---"+s.getage());
        }
    }
}

对应的内存图:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网