请求java问题代码

来源:百度知道 编辑:UC知道 时间:2024/09/27 15:22:16
有一堆形状和颜色不相同的盒子,现在要选出红色正方形盒子和蓝色长方形盒子,请问代码怎么打?

import java.util.ArrayList;
import java.util.List;

public class ShapeColection {
private List<Shape> list = null;

public ShapeColection() {
list = new ArrayList<Shape>();
}

public Shape getShape(int index) {
if (list == null || list.size() == 0)
return null;

return list.get(index);
}

// 给集合添加元素
public void addShape(Shape shape) {
list.add(shape);
}

//
public int getSize() {
return list == null ? 0 : list.size();
}

public static void main(String[] args) {
ShapeColection sc = new ShapeColection();

Shape s1 = new Shape("正方形","红色");
Shape s2 = new Shape("长方形","红色");
Shape s3 = new Shape("正方形","绿色");
Shape s4 = new Shape("长方形","绿色");
Shape s5 = new Shape("三角形","黄色");