【问题标题】:What is the meaning of the static field in AttemptController in following sample code ?以下示例代码中 AttemptController 中的静态字段是什么意思?
【发布时间】:2019-04-27 18:08:34
【问题描述】:

我是 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类设为静态
  • AttemptController??这是 MVC 应用程序吗?如果是,那么拥有这些静态字段是无用的

标签: c# oop static


【解决方案1】:

所以提出了一些更改:

  • 将类设为静态
  • 去掉构造函数,因为静态类不能有实例构造函数。
  • 添加一个名为 init 的新方法,仅用于演示目的。

    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;
            }
        }
    }
    

【讨论】:

    【解决方案2】:

    因为你在构造方法中设置了MaxAttempts,WarningAttempts,Threshold字段。

    当您使用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 字段让所有实例使用相同的字段值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2015-06-17
      • 1970-01-01
      • 2015-06-08
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多