zoukankan      html  css  js  c++  java
  • 只读域

    通过关键字readonly可以设定类中的只读域,即不能修改此域:

    /*
      Example6_3.cs illustrates the use of readonly fields
    */


    // declare the Car class
    public class Car
    {

        // declare a readonly field
        public readonly string make;

        // declare a static readonly field
        public static readonly int wheels = 4;

        // define a constructor
        public Car(string make)
        {
            System.Console.WriteLine("Creating a Car object");
            this.make = make;
        }

    }


    class Example6_3
    {

        public static void Main()
        {

            System.Console.WriteLine("Car.wheels = " + Car.wheels);
            // Car.wheels = 5;  // causes compilation error

            // create a Car object
            Car myCar = new Car("Toyota");

            System.Console.WriteLine("myCar.make = " + myCar.make);
            // myCar.make = "Porsche";  // causes compilation error
            string i = System.Console.ReadLine();
        }

    }

  • 相关阅读:
    使用RPC的接口创建账户同时购买内存并为其抵押CPU和NET资源
    使用RPC的接口创建账户
    【移动安全基础篇】——21、Android脱壳思路
    插件
    NGUI 优化
    影子
    优化文章索引
    MVC
    《你不常用的c#之XX》
    CMake
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2035680.html
Copyright © 2011-2022 走看看