一、实例代码
package edu.aeon.test; /** * [说明]计算机类 * @author aeon */ public class Computer { /**计算机品牌*/ public String brand; /**计算机cpu速度*/ public int cpuSpeed; }
package edu.aeon.test; /** * [说明]学生类 * @author aeon * 类包括静态的属性和动态行为 */ public class Student { /**学生证号*/ private int stuId; /**学生姓名*/ private String stuName; /**学生性别 0(false)-女 1(true)-男*/ private boolean sex; /**学生电脑*/ private Computer computer; /** * 动态行为:学习 */ public void stard(){ System.out.println(stuName+"正在学习!"); } public static void main(String[] args) { /**创建学生对象(实例)*/ Student student=new Student(); System.out.println("==================默认初始化=================="); System.out.println("学生证号:"+student.stuId); System.out.println("学生姓名:"+student.stuName); System.out.println("学生性别:"+student.sex); student.stard(); System.out.println("==================程序初始化=================="); student.stuId=10010; student.stuName="张三"; student.sex=true; System.out.println("学生证号:"+student.stuId); System.out.println("学生姓名:"+student.stuName); System.out.println("学生性别:"+(student.sex==true?"男":"女")); student.stard(); Computer computer=new Computer(); computer.brand="联想"; student.computer=computer; System.out.println(student.computer.brand); } }
二、内存分析图
原文地址:https://www.cnblogs.com/aeon/p/9951570.html
时间: 2024-11-07 09:20:18