标签:
代码:
public class Person {
public void Call(){
Phone phone = new Phone();
}
}public class Phone {
}
解释:Person 和 Phone之间是没有关系的,但是由于偶尔的需要,Person需要使用Phone,在Person类中的具体方法中需要实例化Phone,这时Person就依赖于Phone.持有Phone类的是Person类的一个方法,而不是Person类,这点是最重要的。
public class Person {
//头部是人的一个属性
public Head head;
public Person() {
// TODO Auto-generated constructor stub
}
}public class Head {
public Head() {
// TODO Auto-generated constructor stub
}
}public class PersonGroup {
public Person person;
//将person作为构造方法的参数传进去
public PersonGroup(Person person) {
// TODO Auto-generated constructor stub
this.person = person;
}
}public class Person {
public Person() {
// TODO Auto-generated constructor stub
}
}public class Person {
public Foot foot;
public Person() {
// TODO Auto-generated constructor stub
//在构造方法中实例化
foot = new Foot();
}
}public class Foot {
public Foot() {
// TODO Auto-generated constructor stub
}
}标签:
原文地址:http://blog.csdn.net/it_zjyang/article/details/51355062