【发布时间】:2014-05-18 11:22:50
【问题描述】:
我在一个类中有一个卡片对象hand 的数组列表。这个数组列表hand 必须保持私密。在另一个类中,我需要访问此数组列表中的 Card 对象并在每个对象上调用 toString() 方法以将它们转换为可读的卡片名称。所以我在包含返回手的数组列表hand 的类中创建了一个getHand() 方法。但是我现在如何访问手中的个人牌?因为如果我做 hand.get(i).getSuit() 例如,它不能这样做,因为 hand 是私有的。任何建议表示赞赏。
A类:
private ArrayList<Card> hand;
public Player()
{
ArrayList<Card> hand = new ArrayList<Card>();
}
...
B类:
...
public String toString(){
this.getSuit();
this.getValue();
//code to convert numerical suit and value to a string
//this method will use a Card object
//as an implicit parameter
C类:
Player p = new Player();
p.getHand();
//next I need to indicate that each card
//within hand needs to be called with
//the toString method? This is my question
【问题讨论】:
-
你有没有尝试像你描述的那样做?,而不是
hand使用getHand() -
我不知道你为什么不能那样做,也不知道你为什么在
Player的构造函数中使用局部变量hand