一、对象数组

1、说明

数组的定义类型为对象类型

2、动态初始化

1.声明并开辟空间
  Person[] pers = new Person[长度];
2.赋值
  for(int i=0;i<pers.length;i++){
    pers[i] = new Person();
  }
3.使用
  for(int i=0;i<pers.length;i++){
    pers[i].方法();
  }

二、对象关联

说明

属性的定义类型为对象类型

实例

Person per = new Person(new Comptuter("联想"));


class Person{
  Computer com;//属性为对象类型
  public Person(Computer com){
    this.com = com;
  }
  public String getInfo(){
    return com.brand;
  }
}


class Computer{
  String brand;
  public Computer(String brand){
    this.brand=brand;
  }
}

posted on 2018-08-23 14:55  明德博学勤奋求实  阅读(141)  评论(0编辑  收藏  举报