繁体   English   中英

扩展类,隐式超级构造函数未定义错误

[英]Extending class, implicit super constructor is undefined error

我正在使用Java。 我有2节课

class Card
class Deck extends Card

我收到构造函数错误。

我必须为大学制作二十一点游戏,并且必须扩展Deck班级。

我该怎么做才能解决这个问题?

class Card
{
    int rank, suit;
    String[] suits = { "hearts", "spades", "diamonds", "clubs" };
    String[] ranks  = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

    Card(int suit, int rank)
    {
        this.rank=rank;
        this.suit=suit;
    }

    String toString()
    {
          return ranks[rank] + " of " + suits[suit];
    }

    int getRank() {
         return rank;
    }

    int getSuit() {
        return suit;
    }
}




class Deck extends Card{

    ArrayList <Card> cards;
    int numberdeck;
    Deck()
    {
        cards = new ArrayList<Card>();
        for (int a=0; a<=3; a++)
        {
            for (int b=0; b<=12; b++)
            {
                cards.add( new Card(a,b) );
            }
        }
    }

    Card drawFromDeck()
    {
        Random generator = new Random();
        int index= generator.nextInt( cards.size() );
        return cards.remove(index);
    }

    int getTotalCards()
    {
        return cards.size();
    }
}

子类中的构造函数应该调用超类中的相应构造函数,但是,在您的代码中找不到这样的构造函数。

添加对超类不带任何参数的构造函数,或者使子类中的构造函数调用超类中的构造函数。

您可以使用super(0, 0);调用超类的构造函数super(0, 0); 在子类的构造函数的开头。

Deck真的不应该扩展Card 子分类编码一个is-a关系。 狮子是动物,所以Lion extends Animal 您可以说子类化是不合适的,因为在整个卡片组中,getRank()和getSuit()方法毫无意义。

一副纸牌不是纸牌。 副牌包含其他卡牌。 包含关系已经由ArrayList <Card> cards数组表示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM