• static修饰符


    static(C# 参考)


    使用 static 修饰符声明属于类型本身而不是属于特定对象的静态成员。 static 修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,但不能用于索引器、析构函数或类以外的类型。


    示例
    下面的类声明为 static,并且只包含 static 方法:
    C#

    static class CompanyEmployee
    {
      public static void DoSomething() { /*...*/ }
      public static void DoSomethingElse() { /*...*/ }
    }


    常数或者类型声明隐式地是静态成员。
    不能通过实例引用静态成员。 然而,可以通过类型名称引用它。 例如,请考虑以下类:
    C#

    public class MyBaseC
    {
      public struct MyStruct
      {
        public static int x = 100;
      }
    }

     

    若要引用静态成员 x,请使用完全限定名 MyBaseC.MyStruct.x,除非可从相同范围访问成员:

    C#
    Console.WriteLine(MyBaseC.MyStruct.x);

    尽管类的实例包含该类所有实例字段的单独副本,但每个静态字段只有一个副本。
    不可以使用 this 来引用静态方法或属性访问器。
    如果对类应用 static 关键字,则该类的所有成员都必须是静态的。
    类和静态类可以有静态构造函数。 静态构造函数在程序开始和类实例化之间的某个时刻调用。
    说明
    static 关键字在使用上比在 C++ 中有更多限制。
    为了说明静态成员,请看一个表示公司雇员的类。 假设该类包含一种对雇员计数的方法和一个存储雇员数的字段。 该方法和字段都不属于任何实例雇员, 而是属于公司类。 因此,应该将它们声明为此类的静态成员。
    此示例读取新雇员的姓名和 ID,将雇员计数器加一,并显示新雇员的信息和新的雇员数。 为简单起见,该程序从键盘读取当前的雇员数。 在实际的应用中,应从文件读取此信息。
    C#

    public class Employee4
    {
      public string id;
      public string name;

      public Employee4()
      {
      }

      public Employee4(string name, string id)
      {
        this.name = name;
        this.id = id;
      }

      public static int employeeCounter;

      public static int AddEmployee()
      {
        return ++employeeCounter;
      }
    }

    class MainClass : Employee4
    {
      static void Main()
      {
        Console.Write("Enter the employee's name: ");
        string name = Console.ReadLine();
        Console.Write("Enter the employee's ID: ");
        string id = Console.ReadLine();

        // Create and configure the employee object:
        Employee4 e = new Employee4(name, id);
        Console.Write("Enter the current number of employees: ");
        string n = Console.ReadLine();
        Employee4.employeeCounter = Int32.Parse(n);
        Employee4.AddEmployee();

        // Display the new information:
        Console.WriteLine("Name: {0}", e.name);
        Console.WriteLine("ID: {0}", e.id);
        Console.WriteLine("New Number of Employees: {0}",Employee4.employeeCounter);
      }
    }
    /*
    Input:
    Matthias Berndt
    AF643G
    15
    *
    Sample Output:
    Enter the employee's name: Matthias Berndt
    Enter the employee's ID: AF643G
    Enter the current number of employees: 15
    Name: Matthias Berndt
    ID: AF643G
    New Number of Employees: 16
    */

     

    此示例说明:虽然可以用另一个尚未声明的静态字段实例化一个静态字段,但直到向后者显式赋值后,才能确定结果。
    C#

    class Test
    {
      static int x = y;
      static int y = 5;

      static void Main()
      {
        Console.WriteLine(Test.x);
        Console.WriteLine(Test.y);

        Test.x = 99;
        Console.WriteLine(Test.x);
      }
    }
    /*
    Output:
    0
    5
    99
    */

     

  • 相关阅读:
    python thrift
    redis 知识点
    Spring其他注解和xml配置(不常用的)
    Spring常用的的注解以及对应xml配置详解
    Eureka的工作原理简介
    SpringBoot的自动配置实现和介绍
    SpringBoot多配置文件,切换环境
    数据卷介绍和常用的服务部署
    Spring Security简介
    在Java中入门,读取和创建Excel,Apache POI的使用
  • 原文地址:https://www.cnblogs.com/gsk99/p/4975964.html
Copyright © 2020-2023  润新知