const,static和readonly

来源:互联网 发布:新网域名续费多少钱 编辑:程序博客网 时间:2024/04/28 08:14

定义:

const:它指定字段或局部变量的值是常数,不能被修改。

readonly: 当字段声明包括readonly 修饰符时,该声明引入的字段赋值只能作为声明的一部分出现,或者出现在同一类的构造函数中。

static:声明属于类型本身而不是属于特定对象的静态成员。

 

使用:

const用于定义常量,一般用strut进行存储,并在其他地方直接访问。

readonly用于定义只读的变量,除了在构造函数里,其他地方不允许再对其进行更改。

static用于定义静态的变量,方法或类。静态的类中只能有静态的成员或方法。static是针对于类型,不针对类的实例。即类型直接访问(static的成员或方法必须是public的,类型才能访问),类的实例不能访问。static可以与readonly同时使用。静态方法里通过创建类的实例才可以访问非静态方法;非静态方法里能访问静态变量。静态类中只能有静态方法。

 

举例

    public class Class1
    {
        public static readonly string readonlyValue = "readonlyValue";
        public readonly string readonlyValue2 = "readonlyValue2";
        private static string name = "tong";
        public static string no = "123";
        public string order = "abc";

        public static string getName()
        {

            // return order;                                          访问不到order,因为order不是static,而getName是static
            return name;                                              // 能访问到,因为getName为static
        }

        public string getOrder()
        {
            return order;                                            // 能访问到非静态的变量
        }

        public Class1()
        {
           
        }

        public Class1(bool change)
        {
            //this.readonlyValue = "afterChangeReadonlyValue";       不能访问,因为是静态只读
            this.readonlyValue2 = "afterChangeReadonlyValue2";        // 可以在构造函数中对只读进行修改
        }

        public void chgOnlyTest()
        {
            //this.readonlyValue2 = " ";                             不能在构造函数以外对readonlyValue2赋值    
        }


    }

    public struct ConstValue
    {
        public const string str1 = "str1";                             //const
        public const string str2 = "str2";
        public const string str3 = "str3";
    }

 

 

    class Program
    {
        static void Main(string[] args)
        {
            // *用类型访问
            string no = Class1.no;                 // static属于类型,且no为public,所以能访问到
            //string name = Class1.name;           访问不到name,以为name私有
            //Class1.getOrder();                   访问不到getOrder,以为getOrder不是static,是靠实例的对象来访问的
            string name = Class1.getName();
            //string order = Class1.order;         访问不到order,因为 order为是靠实例的对象来访问的
            string readonlyValue = Class1.readonlyValue;   // static属于类型,且no为public,所以能访问到

            // *实例类访问
            Class1 o_class1 = new Class1(true);
            //string no1 = o_class1.no             访问不到no,因为 no为static  
            //string name = o_class1.name;         访问不到name,以为name私有且为static
            string order = o_class1.order;         //能访问order,以为order为公有,非static
            o_class1.getOrder();                   //能访问order,以为order为公有,非static
            //o_class1.getName();                  访问不到getName(),因为getNme为static
            string readonlyValue2 = o_class1.readonlyValue2;  ////能访问readonlyValue2,以为readonlyValue2为公有,非static,与readonly无关
            //string readonlyValue = o_class1.readonlyValue;  访问不到readonlyValue,因为readonlyValue为static

            // *输出
            Console.Write(no);                          // 123
            Console.Write(name);                        // tong
            Console.Write(ConstValue.str1);
            Console.Write(o_class1.readonlyValue2);     // afterChangeReadonlyValue2,因为readonly可以在构造函数里被改变

            Class1 o_class2 = new Class1();
            Console.Write(o_class2.readonlyValue2);    // readonlyValue2 ,虽然o_class1对readonlyValue2进行了修改,但只是对o_class1有效,不会修改整个class1类的readonlyValue2
        }
    }

 

 

 

原创粉丝点击