【问题标题】:Using another classes function in c#在 C# 中使用另一个类函数
【发布时间】:2017-12-08 01:58:59
【问题描述】:

我试图能够从 dieclass 中调用该函数并在 matchplay 类中使​​用滚动编号...这可能吗?

public class DieClass
{
    public void DiceRoll(int min, int max)
    {
        Random random = new Random();

        int roll1 = random.Next(0, 6);
        int roll2 = random.Next(0, 6);
        int roll3 = random.Next(0, 6);
    }
}

public class MatchPlay
{
    public void Match()
    {
        DieClass.DiceRoll();

        Console.WriteLine("Starting Match Play...");
        Console.WriteLine();
        Console.WriteLine("Round One");
        Console.WriteLine("Your first dice is [0]", roll1);
        Console.WriteLine("Your second dice is [0]", roll2);
        Console.WriteLine("Your third dice is [0]", roll3);
    }
}

}

【问题讨论】:

  • 一个类不是一个对象——更像是一个蓝图。您需要 实例化 和 DieClass 类型的对象才能将其用作对象。您还需要阅读How to Ask 并采取tour
  • 您需要先学习 C# 基础知识。要么制作 DiceRoll 静态方法,要么实例化 DieClass。同时传递最小、最大参数。您需要返回该值才能打印它们。

标签: c# function methods dice


【解决方案1】:

有一些事情需要解决:

  • 您的DiceRoll 方法是一个实例方法,因此您需要创建DiceClass 类的实例才能使用它。
  • roll1roll2roll3 变量是方法的本地变量,因此一旦方法完成,您将无法使用它们。相反,您可以将它们设为类的公共属性。
  • 您不需要在每次调用该方法时都实例化一个新的Random(实际上这会导致问题,因为Random 是使用基于系统时钟的值播种的,所以如果您的方法被调用很快,它将一遍又一遍地产生相同的数字)。您可以将其设为静态并实例化一次。
  • 既然您将minmax 参数传入Roll 方法,我们不应该使用它们吗?您目前有06 硬编码。
  • 要使用格式字符串,您需要使用大括号 ({}) 而不是方括号 ([])。
  • 最后,从命名约定的角度来看,您不需要将单词Class 作为类名的一部分,也不需要将Dice 作为方法名的一部分。这将简化打字量,并且仍然非常容易理解。

您可能会考虑创建一个表示单个Die 对象的类,并为其提供Roll() 方法和Value 属性。然后用户可以根据需要创建任意数量并将它们保存在列表中:

public class Die
{
    public int Value { get; set; }

    // Make this static and instantiate it only once to avoid re-seeding issues
    private static readonly Random rnd = new Random();

    public Die()
    {
        Value = 1;
    }

    public void Roll()
    {
        // This method uses values 1-6 as a standard die
        Roll(1, 6);
    }

    public void Roll(int minValue, int maxValue)
    {
        Value = rnd.Next(minValue, maxValue + 1);
    }
}

现在,您可以按如下方式使用Die 类:

public class MatchPlay
{
    public void Match()
    {
        // Add three Die objects to our list of dice
        List<Die> dice = new List<Die>
        {
            new Die(), new Die(), new Die()
        };

        Console.WriteLine("Starting Match Play...");
        Console.WriteLine();
        Console.WriteLine("Round One");

        // Roll all dice
        dice.ForEach(d => d.Roll());

        Console.WriteLine("Your first dice is {0}", dice[0].Value);
        Console.WriteLine("Your second dice is {0}", dice[1].Value);
        Console.WriteLine("Your third dice is {0}", dice[2].Value);
    }
}

最后,我们可以在 Main 方法中开始匹配:

private static void Main()
{
    MatchPlay game = new MatchPlay();
    game.Match();

    Console.WriteLine("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

【讨论】:

  • 干得好。我也会让 Roll 返回值,但这是一个小问题
  • 哦,这是个好主意。然后你可以同时Roll 和输出Value...Console.WriteLine("After rolling the die, the value is: {0}", dice[0].Roll());
【解决方案2】:

您需要将DiceRoll 设为static 方法,或者创建DieClass 的实例并通过它调用您的方法。

例如,您可以将方法声明为

public static void DiceRoll(int min, int max)

或者你可以像这样实例化一个对象:

DieClass dice = new DieClass();

dice.DiceRoll(0, 6);

话虽如此,您的DieClass 类还有其他问题,其中最明显的是您需要一种将结果传达回调用者的方法。最简单的方法是让DiceRoll() 生成一个结果并返回它。此外,您已将 06 硬编码为 random.Next() 的参数,尽管该方法需要一对参数 minmax

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多