Java 从子类访问ArrayList

Java 从子类访问ArrayList,java,arraylist,Java,Arraylist,我试图从一个子类访问ArrayList,这样我就可以合计游戏中每张牌的值。但是,为了合计这些值,我需要在不创建新方法的情况下访问此ArrayList 这是正在进行的父类: public class Bridge { //instance variables private final String[] SUIT_STRINGS = {"Clubs","Diamonds","Hearts","Spades"}; private final String SUIT_CHARS

我试图从一个子类访问ArrayList,这样我就可以合计游戏中每张牌的值。但是,为了合计这些值,我需要在不创建新方法的情况下访问此ArrayList

这是正在进行的父类:

public class Bridge
{
    //instance variables
    private final String[] SUIT_STRINGS = {"Clubs","Diamonds","Hearts","Spades"};
    private final String SUIT_CHARS = ("CDHS");
    public final int SUIT_AMOUNT = 4; //there are 4 different suits

private Suit[] hand;

////////////////////////////////////////////

public Bridge(String handToProcess)
{
    this.hand = new Suit[SUIT_AMOUNT];
    for (int i = 0; i < SUIT_AMOUNT; i++) //creates the suit classes in the corresponding index
    {
        this.hand[i] = new Suit(SUIT_STRINGS[i]);
    }


    String token;
    int index;


    try
    {
        Scanner textScanner = new Scanner(handToProcess);
        textScanner.useDelimiter(" ");

        while(textScanner.hasNext())
        {
            token = textScanner.next(); //token takes is one of the hands in the line of hands

            index = SUIT_CHARS.indexOf(token.charAt(1)); //set index to the location of the matching suit char
            this.hand[index].addCard(token.charAt(0)); //takes the value of the card and adds it to the hand


        }
    }catch (Exception e)
    {
        System.out.println(e.toString());
    }

}

public void printHand()
{
    System.out.print("Analyzing hand: ");

    String unprocessedCards;
    for (int suitNum = 0; suitNum < SUIT_AMOUNT; suitNum++)
    {
        unprocessedCards = this.hand[suitNum].printSuit();
    }


}

public void printTotalPoints()
{
    int totalPoints = 0;

    for (int suitNum = 0; suitNum < SUIT_AMOUNT; suitNum++)
    {
        totalPoints += this.hand[suitNum].suitPoints();
    }


}
{

私有阵列列表卡;
私有字符串suitStr;
私人最终整数最小长度=5;
私人最终积分=3;
私人最终int单例积分=2;
私人最终int DOUBLETON_分数=1;
//////////////////////////////////////////
公益诉讼(连环诉讼)
{
//套装的最终int位置=1;
/*开关(套件字符(套件位置))
{
案例C:this.suitStr=“Clubs”;
打破
案例“D”:this.suitStr=“Diamonds”;
打破
案例“H”:this.suitStr=“Hearts”;
打破
案例:this.suitStr=“Spades”;
打破
}*/
this.suitStr=套件;
this.cards=new ArrayList();
}
公共无效添加卡(字符面)
{
如果(面>='2'&面最小长度)
{
suitPoints+=this.cards.size()-MIN_LONG;//一套衣服每超过5张牌,就得一分
}
else if(this.cards.size()==2)//如果有2张卡
{
suitPoints+=DOUBLETON_点数;
}
else if(this.cards.size()==1)//如果有一张卡
{
suitPoints+=单例积分;
}
else if(this.cards.size()==0)//如果没有卡
{
suitPoints+=无效_点;
}
返回积分;
}
}

如果不创建某种访问器方法(这在这个特定的赋值中是不允许的),我看不到访问ArrayList卡的方法

类描述

BridgeClient的主要方法是:•创建 允许从hands.txt文件中读取文件中的每一行 o创建桥对象(通过调用适当的构造函数) o调用printHand方法o调用printTotalPoints方法

Card是一个抽象类,表示给定环境中的一张卡 手。它包含:•一个即时变量:卡的o面:2,3, …,9,T,J,Q,K,A•toString和accessor方法•抽象方法 当子类实现时计算值的getPoints 纸牌

CardWithFigure扩展了卡并包含 具有面:T、J、Q、K或A的卡片对象的getPoints方法。It 定义设置为“TJQKA”的最终字符串映射。它使用这个 用于计算点的字符串。请注意,点数 对应于映射中相应字母的位置 字符串:王牌数为4=14-10王牌数为3=13-10 皇后数为2=12–10 插孔计数为1=11–10 十计数为0=10-10

CardWithNumber扩展卡并包含 具有面:2、3、…、或9的卡片对象的getPoints方法

Suit类实现Suit中的一组卡片。它包括:•两项 即时变量:o卡片–一套卡片中卡片对象的数组列表 手部包含o suitStr–此对象的套装名称 代表:梅花、钻石、红桃或黑桃 使用默认ArrayList构造函数实例化卡片,并设置 将suitStr设置为给定值•addCard方法–创建一个 适当的卡片对象并将其添加到卡片列表•打印套装 方法–显示套装中的所有卡片•suitPoints方法- 计算套装中的卡点数

桥类包含:•几个实例变量:o hand– 给定手牌西服字符串的西服对象–名称数组 西服o西服字符–包含每件西服首字母的字符串: “CDH”,可用于将输入套装值映射到 衣服的名字。例如,如果输入是2D–索引“D” 在西服中,CHARS给出了西服字符串中“钻石”的索引 数组•几种方法:o桥(构造函数) 实例化 实例变量hand 取输入行并填充手 使用适当的数据o printHand方法-打印所有四套西装 (请参见样本运行,可能有些西装不适合。) 当前)o printTotalPoints方法-计算 举手


要从子类访问任何字段,请将其声明为protected(非private)

,但是
Suits
不是这里任何东西的子类(当然,除了
java.lang.Object
),您不能从类外使用private变量。撇开技术问题不谈,我不理解您的设计:Suit(e)为什么需要知道卡片列表吗?对我来说,套件更像是一个枚举,卡片列表包含卡片对象,每个卡片对象都属于一个套件。(啊,所以拼写为Suit。)我宁愿将Suit设置为枚举或类似的东西,然后每张卡片都引用一个Suit。IMO套装不是卡片的集合,它是卡片的财产。请看我关于设计的问题。这确实解决了可见性问题,但设计仍然很奇怪。比如说,一套卡片怎么会是桥牌游戏的特例呢?你的设计其实很奇怪。也许你必须重新设计,或者对java和继承有任何误解。也许你更容易准确地解释你想要做什么,也许人们可以帮助你进行设计。我添加了项目的uml图以及我需要实现的类的描述
public class Suit
private ArrayList<Card> cards;
private String suitStr;

private final int MIN_LONG = 5;
private final int VOID_POINTS = 3;
private final int SINGLETON_POINTS = 2;
private final int DOUBLETON_POINTS = 1;

//////////////////////////////////////////

public Suit(String theSuits)
{
    //final int POSITION_OF_SUIT = 1;
    /*switch(theSuits.charAt(POSITION_OF_SUIT))
    {
        case'C': this.suitStr = "Clubs";
        break;
        case'D': this.suitStr = "Diamonds";
        break;
        case'H': this.suitStr = "Hearts";
        break;
        case'S': this.suitStr = "Spades";
        break;
    }*/
    this.suitStr = theSuits;
    this.cards = new ArrayList<Card>();
}

public void addCard(char face)
{
    if (face >= '2' && face <= '9')
    {
        CardWithNumber cardToAdd = new CardWithNumber(face);
        this.cards.add(cardToAdd);
    }
    else
    {
        CardWithFigure cardToAdd = new CardWithFigure(face);
        this.cards.add(cardToAdd);
    }

}

public void printSuit()
{
    //System.out.print(this.suitStr + "\t"); //formatting; may remove later
    for (Card currentCard: this.cards)
    {
        System.out.print(currentCard + " ");
    }
    System.out.println();

}

public int suitPoints()
{
    int suitPoints = 0;

    //check to see if there are more than 5 cards
    if (this.cards.size() > MIN_LONG)
    {
        suitPoints += this.cards.size() - MIN_LONG; //for every card over 5 of a suit you get one point
    }
    else if (this.cards.size() == 2) //if there are 2 cards
    {
        suitPoints += DOUBLETON_POINTS;
    }
    else if (this.cards.size() == 1) //if there is 1 card
    {
        suitPoints += SINGLETON_POINTS;
    }
    else if (this.cards.size() == 0) //if there are no cards
    {
        suitPoints += VOID_POINTS;
    }


    return suitPoints;
}