this、构造器、new

来源:互联网 发布:软件搬家到sd卡的软件 编辑:程序博客网 时间:2024/06/06 17:45

1. 字段:在类中定义的存储数据的成员变量,与包容类型的具名存储单元。包容类型是什么?

    实例字段:在类的级别上声明用于存储与对象相关联的变量。   什么叫在类的级别上?

    关联:是字段类型与包容类型之间的关联。

             PS:字段不包含static修饰符则表示是实例字段。

2. this的用法

      this指当前类的字段或方法,传递对当前正在执行的对象的引用。 在该类方法中调用:若其方法的形参与该类字段同名,则使用 this.字段名 区别于方法的局部变量。也可在其方法中调用该类中的其他方法:this.方法名  (跟其他语言中调用this差不多)

 class Program
    {
        static void Main()
        {
            Other other = new Other();
            other.firstName= "Zhang";
            other.lastName = "Yixing";
            other.SetName(other.firstName, other.lastName);
            System.Console.ReadLine();
        }
    }

    class Other
    {
        public string firstName;   //实例字段
        public string lastName;
        
        public string GetName()
        {
            return firstName + " " + lastName;
        }
        public void SetName(string firstName, string lastName)
        {
            this.firstName = firstName;
            // firstName = firstName;    也是正确的写法     

           this.lastName = lastName;
            Console.WriteLine("The guy's fullName  is : {0}!",GetName());   // 写成this.GetName()也是正确的
        }
    }


3. 构造器(初始化对象实例的方法)

      实例化对象后即使未立刻对字段进行初始化,编译器也不会警告,最终则会得到含有无效数据的对象。而构造器是提供一种在创建对象时就指定必需数据的方式。

      定义构造器:创建一个与类名完全相同的且没有返回类型的方法。

      字段在声明中又在构造器内部进行赋值操作,那么只有在声明时赋值发生之后构造器内部的赋值才会发生,所以,最终生效的时构造器内部的赋值。

      默认构造器:编译器自动添加,不获取参数。

     一个类中可以采用方法重载的方式含有多个构造器。但首选方式采用可选参数,可不是重载方法。

     构造器链:使用this在一个构造器中调用另一个构造器。

  class Other
    {
        public Other(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
        }
        public Other(int ID, string firstName,string lastName): this(firstName, lastName)                 //注意此时this的写法
        {
            Id = ID;
        }
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

下图是报错的写法:具体原因见下图的图中说明

   匿名类型:

class Program
    {
        static void Main()
        {
            var Other1 =
                new
                {
                    Title= "BBB",
                    Name= "huang"

                };
           var Other2 =
                new
                {
                    Title="CCC",
                    Name="li"
                };
            var Other3 =
               new
               {
                   Title = Other1.Title,
                   Name = Other2.Name
               };
            Console.WriteLine("output:other1.Title={0},other1.Name={1}", Other1.Title, Other1.Name);
            Console.WriteLine("output:other3:{0}", Other3);
            System.Console.ReadLine();
        }
    }

输出结果:

错误写法:除var以外的数据类型军不支持


4. new操作符的实现细节:

       (1)、 从内存管理器获取“空白”内存。

       (2)、调用指定的构造器,将对“空白“内存的引用作为隐式的this参数传递给构造器

       (3)、构造器链剩余部分开始执行,在构造器之间传递引用。

       (4)、构造器链上的执行结束后,new操作符返回内存引用。现在,该引用指向的内存处于初始化好的形式。

参考:http://blog.csdn.net/donetren/article/details/6509248