JAVA学习笔记-内存分析
配合着上图的代码块如下:
首先写一个TestCompuer类
package Study;
public class TestComputer {
String brand;
int cpuNumber;
}
TestStudent类:
package Study;
public class TestStudent {
//静态的数据
String name;
int id;
int age;
int achievement;
TestComputer computer;
//动态的行为
public void SayHello(String toWho){
System.out.println(name+"对"+toWho+"说:您好!");
}
public void ShowAchievement(){
System.out.println(name+"的分数为:"+achievement);
}
public static void main(String[] args){
TestStudent mouren = new TestStudent();
mouren.name ="我";
mouren.id = 01 ;
mouren.age = 22;
mouren.achievement = 100;
mouren.SayHello("java");
mouren.ShowAchievement();
TestComputer pc = new TestComputer();
pc.brand = "联想";
pc.cpuNumber = 4;
mouren.computer = pc;
pc.brand = "Dell";
System.out.println(mouren.computer.brand);
}
}
//因为mouren.computer.brand被指向了pc.brand 当改变pc.brand得值得时候,mouren.computer.brand的值也会改变。所以输出结果是Dell,而不是联想。