【问题标题】:Stack Overflow Error: Using constructors in base class and extended class?堆栈溢出错误:在基类和扩展类中使用构造函数?
【发布时间】:2021-03-12 14:55:58
【问题描述】:

我对 Java 很陌生,但我正在尝试修复这个堆栈溢出错误。本质上,我正在尝试使用 main 中的构造函数调用来创建一副纸牌:Deck newDeck = new Deck();

然后,在基类“Deck”中创建包含 52 个元素(卡片)的 Cards 数组,并在数组的每个索引处填充两个 int 值,代表每张卡片的点数和花色。

我相信堆栈溢出错误是由于基类和扩展类之间的递归构造函数调用而发生的,尽管我可能错了。谁能提供有关如何修复此错误的建议,或以不同的方式填充 Cards 数组?我的项目需要基类和扩展类格式。谢谢。

错误:

Exception in thread "main" java.lang.StackOverflowError
at java.base/java.util.Random.<init>(Random.java:105)
at Deck.<init>(Deck.java:12)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)
at Deck.<init>(Deck.java:27)
at Cards.<init>(Cards.java:19)

主要:

 import java.util.Random;
import java.util.Scanner;

public class PokerProgram {
    public static void main(String[] args) {

        // CREATING SCANNER OBJECT
        Scanner scnr = new Scanner(System.in);
        Random random = new Random();

        // INITIALIZING VARIABLES
        String play = "y";
        double blind = 10;
        double playerBalance = 1000;
        double playerBet;
        double pot;
        int playerScore = 0;
        int compScore = 0;
        String[] Hands = {"Equal hand", "One Pair", "Two Pair", "Three of a Kind", "Straight", "Flush", "Full House", "Four of a Kind", "Straight Flush", "Royal Flush"};
        Deck newDeck = new Deck();

基类:

 import java.util.Random;
    
    public class Deck{
        // Creating Random object
        Random random = new Random();
    
        // FIELD VARIABLES
    
        public static int handScore;
        public static int cardValue;
        private static final Cards[] newDeck = new Cards[52];
    
        // CONSTRUCTORS
    
        // For Deck
        public Deck () {
            int i = 0;
            for (int j = 0; j < 13; j++) {
                for (int k = 0; k < 4; k++) {
                    newDeck[i] = new Cards(j, k);
                    i++;
                }
            }
        }

扩展类:

public class Cards extends Deck{

// Field Variables
private int rank;
private int suit;

// Rank and Suit Arrays
private static String [] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
private static String [] suits = {"Hearts", "Diamonds", "Clubs", "Spades"};

// CONSTRUCTOR
public Cards (int rank, int suit) {
    this.rank = rank;
    this.suit = suit;
}

【问题讨论】:

  • 永远不要在超类中使用子类。
  • Cards 类扩展了Deck,因此每次创建Cards 时,您也在创建Deck,从而创建Cards 等。
  • 所以它是递归的。谢谢!
  • 虽然其他答案/cmets 说 what 是错误的,但这并没有说明 why 它是错误的或如何解决它。在这里,您的 Cards 类代表 single Card(有等级和花色)。如果您将其命名为Card,那么逻辑会变得更简单。 Card 不是Deck 的扩展(Deck 也不是Card 的扩展)。 Deck一些Cards。所以在你的类层次结构中没有extends

标签: java class recursion constructor stack-overflow


【解决方案1】:

卡牌扩展牌组。在创建 Deck 时,您调用 Cards 构造函数,该构造函数恰好是调用 Cards 构造函数的 Deck...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 2012-06-15
    • 1970-01-01
    • 2013-11-15
    • 2021-02-13
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多