private ArrayList<Card> cardList = new ArrayList<Card>();如何在cardList中添加card的关联类Shape类的对象
时间: 2023-11-27 12:02:57 浏览: 123
intent传递ArrayList<T>
可以通过在Card类的构造函数中接受Shape类的对象作为参数,并在构造函数内将其保存到Card类的成员变量中,然后在向cardList中添加Card对象时,传递Shape对象作为参数即可。具体实现可以参考以下代码:
```
public class Card {
private Shape shape;
public Card(Shape shape) {
this.shape = shape;
}
// getter and setter methods for shape
// other methods for Card class
}
public class Shape {
// fields, methods, constructors for Shape class
}
// create a Shape object
Shape shape = new Shape();
// create a Card object using the Shape object
Card card = new Card(shape);
// add the Card object to the cardList
cardList.add(card);
```
阅读全文