对象关联
[TOC]
含义
一个对象 又引用了另一个对象
其实就是 一个类中的属性类型 为引用类型
内存图
image代码
//案例 教师使用电脑
package been;
public class Teacher {
private String name;
private Computer computer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {//Computer 类型
this.computer = computer;
}
public String getInfo(){
return name +"\t"+computer.getDetails();
}
}
package demo1;
import been.Computer;
import been.Teacher;
public class TestObjectRelation {
public static void main(String[] args) {
Teacher t = new Teacher();
t.setName("xuehaozhe");
//对象关联 先创建对象 再使用
Computer c = new Computer();
c.setBrand("MAC");
c.setPrice(10000);
t.setComputer(c);
//System.out.println(t.getInfo());
//xuehaozhe been.Computer@41aff40f
System.out.println(t.getInfo());
}
}
网友评论