经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Memory map of an object array
来源:cnblogs  作者:小步快跑!!!!!  时间:2018/10/16 9:40:06  对本文有异议
  1. Student类:
  2. package com.itheima;
  3. /*
  4. * 自动生成构造方法:
  5. * 代码区域右键 -- Source -- Generate Constructors from Superclass... 无参构造方法
  6. * 代码区域右键 -- Source -- Generate Constructor using Fields... 带参构造方法
  7. * 自动生成getXxx()/setXxx():
  8. * 代码区域右键 -- Source -- Generate Getters and Setters...
  9. */
  10. public class Student {
  11. private String name;
  12. private int age;
  13. public Student() {
  14. }
  15. public Student(String name, int age) {
  16. this.name = name;
  17. this.age = age;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public int getAge() {
  26. return age;
  27. }
  28. public void setAge(int age) {
  29. this.age = age;
  30. }
  31. }
  1. Student的测试类:
  2. package com.itheima;
  3. /*
  4. * 创建一个学生数组,存储三个学生对象并遍历
  5. *
  6. * 分析:
  7. * A:定义学生类
  8. * B:创建学生数组
  9. * C:创建学生对象
  10. * D:把学生对象作为元素赋值给学生数组
  11. * E:遍历学生数组
  12. */
  13. public class StudentDemo {
  14. public static void main(String[] args) {
  15. //创建学生数组
  16. Student[] students = new Student[3];
  17. //创建学生对象
  18. Student s1 = new Student("曹操",40);
  19. Student s2 = new Student("刘备",35);
  20. Student s3 = new Student("孙权",30);
  21. //把学生对象作为元素赋值给学生数组
  22. students[0] = s1;
  23. students[1] = s2;
  24. students[2] = s3;
  25. //遍历学生数组
  26. for(int x=0; x<students.length; x++) {
  27. Student s = students[x];
  28. //System.out.println(s);
  29. System.out.println(s.getName()+"---"+s.getAge());
  30. }
  31. }
  32. }

对应的内存图:

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号