以下是示例代码中AttemptController中静态字段的含义是什么?

3

我是一个C#的新手,正在尝试学习静态关键字。我不明白为什么我们需要两次初始化静态字段。按照我的理解,静态字段在程序执行期间保留值。

class Program
    {
        static void Main(string[] args)
        {

        AttemptController Obj = new AttemptController(3, 2);
        Console.WriteLine("Maximum:   {0}", AttemptController.MaxAttempts);
        Console.WriteLine("Warning:   {0}", AttemptController.WarningAttempts);
        Console.WriteLine("Threshold: {0}", AttemptController.Threshold);

        AttemptController Obj1 = new AttemptController(7, 5);
        Console.WriteLine("Maximum:   {0}", AttemptController.MaxAttempts);
        Console.WriteLine("Warning:   {0}", AttemptController.WarningAttempts);
        Console.WriteLine("Threshold: {0}", AttemptController.Threshold);
        Console.ReadLine();
    }

    class AttemptController
    {
        internal static int MaxAttempts;
        internal static int WarningAttempts;
        internal static int Threshold;

        public AttemptController(int a, int b)
        {
            MaxAttempts = a;
            WarningAttempts = b;
            Threshold = MaxAttempts - WarningAttempts;
        }
    }
}

尝试将 AttemptController 类设为静态。 - jazb
AttemptController?? 这是MVC应用程序吗?如果是,那么拥有那些静态字段是无用的。 - Rahul
2个回答

2
所以有几个建议更改:

  • make the class static
  • get rid of the constructor as static classes cannot have instance constructors.
  • add a new method called init just for demo purposes.

    using System;
    
    namespace ConsoleApp4
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                AttemptController.Init(3, 2);
                Console.WriteLine("Maximum:   {0}", AttemptController.MaxAttempts);
                Console.WriteLine("Warning:   {0}", AttemptController.WarningAttempts);
                Console.WriteLine("Threshold: {0}", AttemptController.Threshold);
    
                AttemptController.Init(7, 5);
                Console.WriteLine("Maximum:   {0}", AttemptController.MaxAttempts);
                Console.WriteLine("Warning:   {0}", AttemptController.WarningAttempts);
                Console.WriteLine("Threshold: {0}", AttemptController.Threshold);
                Console.ReadLine();
        }
    }
    
        public static class AttemptController
        {
            internal static int MaxAttempts;
            internal static int WarningAttempts;
            internal static int Threshold;
    
    
    
            public static void Init(int a, int b)
            {
                MaxAttempts = MaxAttempts + a;
                WarningAttempts = WarningAttempts + b;
                Threshold = MaxAttempts - WarningAttempts;
            }
        }
    }
    

1
因为你在构造方法中设置了MaxAttemptsWarningAttemptsThreshold字段。
当你使用AttemptController Obj = new AttemptController(3, 2);时,它将设置这些值。
当你使用时,将会设置MaxAttempts = 3WarningAttempts = 2
AttemptController Obj = new AttemptController(3, 2);

当您使用 MaxAttempts = 7WarningAttempts = 5 时,将设置。
AttemptController Obj1 = new AttemptController(7, 5);

static字段允许所有实例使用相同的字段值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接