package test;
import java.util.List;
import java.util.ArrayList;
public class Fanxintest {
public static void main(String[] args) {
List<Animal11> list = new ArrayList<>();//list中只有Animal11的对象
Animal11 dog = new Animal11("小狗","白色");
Animal11 cat = new Animal11("小猫","绿色");
Animal11 rabbit = new Animal11("小兔子","银色");
list.add(dog);
list.add(cat);
list.add(rabbit);
System.out.println("list的大小"+list.size());//添加了几个对象
for(Animal11 a :list){//用foreach方法输出list中所有的对象
System.out.println(a.getName()+":"+a.getColor());
}
}
}
class Animal11{
String name;
String color;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}
public Animal11(String name,String color){
this.name = name;
this.color = color;
}
}
运行结果:
list的大小3
小狗:白色
小猫:绿色
小兔子:银色