在测试中打印 toString() 方法输出 class

printing a toString() method output in a test class

大家好,我一直在为我的文字益智游戏进行测试 class,输出是打印对象的对象引用编号。任何人都得到了打印对象的 return 语句的解决方案。

输出:

Generator stats: word-puzzles generated from words of length 3 Puzzle.WordPuzzleGenerator@c68c3Puzzle.WordPuzzleGenerator@b2002fPuzzle.WordPuzzleGenerator@2a4983Puzzle.WordPuzzleGenerator@406199Puzzle.WordPuzzleGenerator@c7b00cPuzzle.WordPuzzleGenerator@1f6f296Puzzle.WordPuzzleGenerator@1df5a8fPuzzle.WordPuzzleGenerator@b2a2d8Puzzle.WordPuzzleGenerator@1e13d52Puzzle.WordPuzzleGenerator@80fa6f

测试Class

public class Test_WordPuzzleGenerator {
    public static void main(String[] args) throws FileNotFoundException {

        int sizeTest1 = 3;
        System.out
                .println("Generator stats: word-puzzles generated from words of length "
                        + sizeTest1);

        for (int i = 0; i < 10; i++) {
            WordPuzzleGenerator puzzle = new WordPuzzleGenerator(sizeTest1);
            System.out.print(puzzle);
        }

        int sizeTest2 = 3;
        System.out
                .println("Generator stats: word-puzzles generated from words of length "
                        + sizeTest2);
        for (int i = 0; i < 10; i++) {
            new WordPuzzleGenerator(sizeTest2);
        }
    }
}

主程序:

public class WordPuzzleGenerator {
    static ArrayList<String> wordList = new ArrayList<String>();

    public WordPuzzleGenerator(int size) throws FileNotFoundException {
        ArrayList<String> puzzleListY = new ArrayList<String>();
        ArrayList<String> puzzleListX = new ArrayList<String>();
        String randomXWord;
        String letterSize = "" + size;
        makeLetterWordList(letterSize);
        boolean finished = false;
        while ( !finished ) {
            finished = true;
            puzzleListX.clear();
            puzzleListY.clear();
            for (int i = 0; i < size; i++) {
                int randomYWord = randomInteger(wordList.size());
                String item = wordList.get(randomYWord);
                puzzleListY.add(item);
            }
            for (int i = 0; i < puzzleListY.size(); i++) {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < puzzleListY.size(); j++) {
                    sb.append(puzzleListY.get(j).charAt(i));
                }
                randomXWord = sb.toString();
                if (!wordList.contains(randomXWord) && !puzzleListY.contains(randomXWord)) {
                    finished = false;
                    break;
                }
                puzzleListX.add(randomXWord);

            }




        }
        toString(puzzleListX, puzzleListY);
    }

    public static int randomInteger(int size) {
        Random rand = new Random();
        int randomNum = rand.nextInt(size);
        return randomNum;
    }



    public static void makeLetterWordList(String letterSize) throws FileNotFoundException {

        Scanner letterScanner = new Scanner( new File (letterSize + "LetterWords.txt"));
        wordList.clear();
        while (letterScanner.hasNext()){
            wordList.add(letterScanner.next());
        }
        letterScanner.close();
    }

    public static String toString(ArrayList<String> ArrayList1, ArrayList<String> ArrayList2){
        StringBuilder group1 = new StringBuilder();
        for (int i = 0; i < ArrayList1.size(); i++) {
            group1.append(ArrayList1.get(i) + " ");
        }
        String wordsInString1 = group1.toString();

        StringBuilder group2 = new StringBuilder();
        for (int i = 0; i < ArrayList2.size(); i++) {
            group2.append(ArrayList2.get(i) + " ");
        }
        String wordsInString2 = group2.toString();

        return String.format("\t( %s) ( %s)", wordsInString1, wordsInString2);
    }
}

您的 WordPuzzleGenerator class 不会覆盖 ObjecttoString。相反,它包含一个具有不同签名的静态 toString 方法。

您的 WordPuzzleGenerator class 中需要此签名的方法 :

@Override
public String toString()
{
    ...
}

再看一看,您的 WordPuzzleGenerator 似乎只有静态方法而没有实例成员,因此不清楚您对 toString 到 return 的期望,或者换句话说 -不清楚 System.out.print(puzzle); 预期打印什么。

编辑:

如果你想 toString() 打印在你的构造函数中创建的列表,你应该使它们成为实例成员:

ArrayList<String> puzzleListY;
ArrayList<String> puzzleListX;
public WordPuzzleGenerator(int size) throws FileNotFoundException {
    puzzleListY = new ArrayList<String>();
    puzzleListX = new ArrayList<String>();
    ...
}

然后你可以像这样覆盖 toString :

@Override
public String toString()
{
    return WordPuzzleGenerator.toString (puzzleListX,puzzleListY);
}

您必须覆盖对象的 toString 方法,因为您的对象继承自 java object

@Override
public String toString(){
\mystring build up...
return mystring;

注意覆盖注释,这就是诀窍;) 快乐编码!

尝试重写 class 中的 'toString' 方法,如下所示:

@Override
public String toString()
{
    //your code
}