java - 程序中的子类出了什么问题?

标签 java inheritance

我最近创建了一个 Deck 类(这是父类(super class)),我正在尝试创建一个测试器方法来查看我的类是否有效。 这是我的套牌类别:

import java.util.List;
import java.util.ArrayList;

/**
 * The Deck class represents a shuffled deck of cards.
 * It provides several operations including
 *      initialize, shuffle, deal, and check if empty.
*/
public class Deck 
{
/**
 * cards contains all the cards in the deck.
 */
public static List<Card> cards;

/**
 * size is the number of not-yet-dealt cards.
 * Cards are dealt from the top (highest index) down.
 * The next card to be dealt is at size - 1.
 */
private int size;
public static Card cardOne;

/**
 * Creates a new <code>Deck</code> instance.<BR>
 * It pairs each element of ranks with each element of suits,
 * and produces one of the corresponding card.
 * @param ranks is an array containing all of the card ranks.
 * @param suits is an array containing all of the card suits.
 * @param values is an array containing all of the card point values.
 */
public Deck(String[] ranks, String[] suits, int[] values) 
{
    for(int i=0; i<13;i++)
    {
        suits[i] = "Heart";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }
    for(int i=0; i<13;i++)
    {
        suits[i] = "Spade";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }
    for(int i=0; i<13;i++)
    {
        suits[i] = "Club";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }
    for(int i=0; i<13;i++)
    {
        suits[i] = "Diamond";
        ranks[i] = cardOne.rank();
        values[i] = cardOne.pointValue();
        cards.add(cardOne);
    }

}


/**
 * Determines if this deck is empty (no undealt cards).
 * @return true if this deck is empty, false otherwise.
 */
public static boolean isEmpty() 
{
    if(cards.size()==0)
        return true;
    else
        return false;
}

/**
 * Accesses the number of undealt cards in this deck.
 * @return the number of undealt cards in this deck.
 */
public static int size() 
{
    return  cards.size();
}

/**
 * Randomly permute the given collection of cards
 * and reset the size to represent the entire deck.
 */
public static List<Card> Shuffled[];
public void shuffle() 
{
    for(int i=0; i<52; i++)
    {
        cards.get(i);

        int k=(int)(Math.random()*100);
        while(k >52 || k<0)
        {
            k=(int)(Math.random()*100);
        }
        if(Shuffled[k]==null)
            Shuffled[k]=(List<Card>) cards.get(i);
    }

}

/**
 * Deals a card from this deck.
 * @return the card just dealt, or null if all the cards have been
 *         previously dealt.
 */
public Card deal() 
{
    int cardDealed= (int)(Math.random()*100);
    while(cardDealed >52 || cardDealed<0)
    {
        cardDealed=(int)(Math.random()*100);
    }
    Shuffled[cardDealed].remove(cardDealed);

    return (Card) Shuffled[cardDealed];
}

/**
 * Generates and returns a string representation of this deck.
 * @return a string representation of this deck.
 */
@Override
public String toString() 
{
    String rtn = "size = " + size + "\nUndealt cards: \n";

    for (int k = size - 1; k >= 0; k--) {
        rtn = rtn + cards.get(k);
        if (k != 0) {
            rtn = rtn + ", ";
        }
        if ((size - k) % 2 == 0) {
            // Insert carriage returns so entire deck is visible on console.
            rtn = rtn + "\n";
        }
    }

    rtn = rtn + "\nDealt cards: \n";
    for (int k = cards.size() - 1; k >= size; k--) {
        rtn = rtn + cards.get(k);
        if (k != size) {
            rtn = rtn + ", ";
        }
        if ((k - cards.size()) % 2 == 0) {
            // Insert carriage returns so entire deck is visible on console.
            rtn = rtn + "\n";
        }
    }

    rtn = rtn + "\n";
    return rtn;
   }
}

我无法弄清楚如何组合 DeckTester 类(它是子类)。到目前为止,我有以下内容:

import java.util.List;

/**
* This is a class that tests the Deck class.
*/
public class DeckTester extends Deck
{
   public static List<Card> cards;

   public DeckTester(String[] ranks, String[] suits, int[] values) 
   {
        super(ranks, suits, values);
   }

  /**
  * The main method in this class checks the Deck operations for    consistency.
  * @param args is not used.
  */
  public static void main(String[] args) 
  {

  }
}

我还有一个功能齐全的 Cards 类。我只是不知道如何检查 Deck 类。

最佳答案

我可以立即说的是,tested类和tester类通过composition组合在一起不是通过继承

所以你必须:

  1. tested类导入tester
  2. 创建tested类的实例
  3. 执行已测试对象的一些逻辑
  4. 检查它是否符合您的期望,例如 JunitTestNG

关于java - 程序中的子类出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36251911/

相关文章:

java - 使用我的类而不是 JAR 中的类

java - Jsf El 表达式 getter 和 setter ArrayList

java - 在java中访问继承的类变量

c++ - 在两个子类中使用基本乘法运算符

c++ - 我怎样才能让一个派生类的对象的 "reference"成员成为基类的一个实例?

java - iTunes App Store 接受非 native 应用程序吗?

java - tomcat ssl客户端握手java错误

java - 比构造函数快?

ruby - 在类中包含模块并执行代码

c++ - 如何声明两个不同的静态变量? (C++)