4

当我尝试在 Deck 类中调用我的 ToString() 方法时,它给了我一个“NullReferenceException 未处理”错误。做了一些测试,我发现当它试图调用Deck.ToString()方法时,大小设置为0。它应该是在调用构造函数时设置的,我不知道在重置什么它。

主要方法:

public class DeckTest
{

    public static void Main()
    {
        Deck toTest = null;
        Console.WriteLine("What type of Deck would you like to create?");
        toTest = new Deck(Console.ReadLine());

        Console.WriteLine(toTest);

    }

}

甲板类:

class Deck
{
    String type;
    int size;
    String deckList;
    Card[] deck;

    public Deck(String type, int size)
    {
        deck = new Card[size];
        this.type = type;
        this.size = size;

       while (deck[size - 1] == null)
       {

           Card newCard;
           Console.WriteLine("Please Enter the Type, Name, Colour (if card has no Colour, enter 'Colourless'), Colour Identity, Text, Mana Cost (if applicable), Converted Mana Cost (if applicable), Power (if applicable), Toughness (if applicable), separated by semicolons.");
           String line = Console.ReadLine();
           string[] card = line.Split(';');
           if (card[0].Equals("Land", StringComparison.OrdinalIgnoreCase))
           {
               newCard = new Card(card[0], card[1], card[3], card[4]);
           }
           else if (card[0].Equals("creature", StringComparison.OrdinalIgnoreCase))
           {
               newCard = new Card(card[0], card[1], card[2], card[3], card[4], card[5], int.Parse(card[6]), int.Parse(card[7]), int.Parse(card[8]));
           }
           else
           {
               newCard = new Card(card[0], card[1], card[2], card[3], card[4], card[5], int.Parse(card[5]));
           }
           addCard(newCard);
           }
   }


    public Deck(String type)
    {
        if (type.Equals("Standard", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 60);
        }
        else if (type.Equals("Extended", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 60);
        }
        else if (type.Equals("Modern", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 60);
        }
        else if (type.Equals("Commander", StringComparison.OrdinalIgnoreCase)|| type.Equals("EDH", StringComparison.OrdinalIgnoreCase))
        {
            new Deck(type, 100);
        }
    }

    void addCard (Card newCard)
    {
        int count = 0;

        while (deck[count] != null && count < size)
        {
            count++;
        }

        if (count < size)
        {
            deck[count] = newCard;
        }
        else
        {
            Console.WriteLine("This deck is full.");
        }
    }

    public override string ToString()
    {
        String output = "";
        int count = 0;

        while (deck[count] != null && count < size-1)
        {
            output += deck[count] + "/n";
        }

        return output;
    }

}
4

3 回答 3

3

new关键字是创建一个全新的实例(即被立即丢弃),而不是修改构造函数中的现有实例。

您应该使用工厂模式(一种根据参数实例化不同对象然后返回它的静态方法):

public static Deck FromType(String type)
{
    if (type.Equals("Standard", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 60);
    }
    else if (type.Equals("Extended", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 60);
    }
    else if (type.Equals("Modern", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 60);
    }
    else if (type.Equals("Commander", StringComparison.OrdinalIgnoreCase)|| type.Equals("EDH", StringComparison.OrdinalIgnoreCase))
    {
        return new Deck(type, 100);
    }

    throw new ArgumentOutOfRangeException("Bad type");
}

toTest = Deck.FromType(Console.ReadLine())然后,您可以使用而不是使用new关键字来实例化您的新 Deck 。

于 2012-09-28T03:07:58.460 回答
2

我会说这不是获取对象的最佳方法,您可以尝试以下方法:

public class Deck
{
    public static Deck GetDeck(String type)
    {
        if (type.Equals("Standard", StringComparison.OrdinalIgnoreCase))
        {
            return new Deck(type, 60);
        }
        else if (type.Equals("Extended", StringComparison.OrdinalIgnoreCase))
        {
            return new Deck(type, 100);
        }
    }
}

而不是像这样获得甲板:

Deck.GetDeck("foo");
于 2012-09-28T03:12:07.863 回答
1

要调用另一个构造函数,请在方法主体之前而不是在其内部执行此操作:

public Deck(String type) : this(type,
  type.Equals("Standard", StringComparison.OrdinalIgnoreCase) ||
  type.Equals("Extended", StringComparison.OrdinalIgnoreCase) ||
  type.Equals("Modern", StringComparison.OrdinalIgnoreCase)
? 60 : 100)
{
  // nothing more here
}
于 2012-09-28T03:16:23.323 回答