简体   繁体   English

如何在2d数组的多重构造函数中覆盖ToString?

[英]How can I override ToString in multi constructor with 2d array?

A question from a C# beginner that can't find the solution in web. 来自C#初学者的问题,无法在Web中找到解决方案。

Here is a constructor with an 2d array 这是带有二维数组的构造函数

public MineSweeperBoard() { // new board board = new BoardItem[MineSweeper_Constants.RowNum, >MineSweeper_Constants.ColNum]; public MineSweeperBoard(){//新的木板= new BoardItem [MineSweeper_Constants.RowNum,> MineSweeper_Constants.ColNum]; // put mines and set value InitialiseBoard(); //放置地雷并设置值InitialiseBoard();
} }

private BoardItem [,] board;

board.ToString is not working that it return the type instead of override it. board.ToString无法正常工作,它返回类型而不是覆盖它。

public override string ToString()
{
    string a = "";
    for (int j = 0; j < MineSweeper_Constants.ColNum; j++)
    {
        for (int i = 0; i < MineSweeper_Constants.RowNum; i++)
        {
            a += board[i, j].ToString();
        }
    }
}

Here is another constructor BoardItem: 这是另一个构造器BoardItem:

public BoardItem(char a)
{
    this.itemContents = a;
    this.visibleItemContents = ProjectTwo.MineSweeper_Constants.character[10];
}

Here BoardItem.ToString() is working. 这里BoardItem.ToString()正在工作。

public override string ToString()
{
    char[] b = new char[1];
    b[0] = GetVisibleItemContents();
    string a = new string(b);
    return a;            
}

How can I print the string of board with override ToString(my teacher said I must use this)? 如何用覆盖ToString打印板子的字符串(我的老师说我必须使用此字符串)?

>{
>    class BoardItem
>    {
>        readonly char itemContents;
>        char visibleItemContents;
>
>        public BoardItem(char a)
>        {
>            // 1. The actual contents of the item (use Minesweeper_Constants) 
>            this.itemContents = a;
>
>            // 2. The visible contents of the item (use Minesweeper_Constants) and set to >unknown
>            this.visibleItemContents = ProjectTwo.MineSweeper_Constants.character[10];
>        }
>
>        public char GetItemContents()
>        {
>            return this.itemContents;
>        }
>
>        public override string ToString()
>        {
>                char[] b = new char[1];
>               b[0] = GetVisibleItemContents();
>                string a = new string(b);
>                return a;
>            
>        }
>   
>    }
>}
>

another class 另一堂课

enter code here

>    class MineSweeperBoard
>    {
>        private BoardItem [,] board;
>        protected static int[,] mineLocation;
>        protected static int numberOfMine;
>public MineSweeperBoard()
>        {
>            // new board
>            board = new BoardItem[MineSweeper_Constants.RowNum, >MineSweeper_Constants.ColNum];
>            // put mines and set value
>            InitialiseBoard();            
>        }
>public override string ToString()
>{
>    string a = "";
>    for (int j = 0; j < MineSweeper_Constants.ColNum; j++)
>    {
>        for (int i = 0; i < MineSweeper_Constants.RowNum; i++)
>        {
>            a += board[i, j].ToString();
>        }
>    }
>}        

main class: 主类:

enter code here

>static void Main(string[] args){
>GameLoop()
>}
>GameLoop(){
MineSweeperBoard board = new MineSweeperBoard();
>Console.WriteLine(board.ToString());
>}

You could do something like this: 您可以执行以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Board
{
    class Program
    {
        static void Main(string[] args)
        { 
            // Create the board
            Board b = new Board();
            // Print the maze
            Console.Write(b.ToString());
            Console.ReadKey();
        }
    }

    class Board
    {
        private BoardItem[,] items = new BoardItem[10, 10];

        // Board contructor - populate the multidimensional array - do you own login here to populate the cells.
        public Board()
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {                        
                    items[i, j] = new BoardItem();
                }
            }
        }

        // Print each cell from the MineSweeper
        public override string ToString()
        {
            string ret = "";
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    ret += items[i, j].ToString();
                }
                ret += Environment.NewLine;
            }

            return ret;
        }
    }

    class BoardItem
    {
        char BoardItemCharToPriint = '-';

        // Print the current cell
        public override string ToString()
        {
            return BoardItemCharToPriint.ToString();
        }
    }
}

As you can see I created a Board class encapsualting the board items. 如您所见,我创建了一个包含类的Board类的Board类。 Each class has it's ToString() method overridden. 每个类都有其ToString()方法被覆盖。 If you go something like this you can make your task as your teacher wanted, by overriding the ToString methods. 如果您执行类似的操作,则可以通过重写ToString方法来按老师的要求完成任务。

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

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