《C#入门详解》刘老师 字段&属性&索引器&常量

字段&属性&索引器&常量

字段

  • 什么是字段

          1.字段是一种表示与对象或类型(类与结构提)关联的变量

          2.字段是类型的成员

          3.与对象关联的字段称“实例字段”

          4.与类型关联的字段称为“静态字段”,由static修饰,表示这个类型当前的状态

            例子:

            console.writeline(student.Amount);

            class student{public int age=20;public static int amount; static student(){student.amount=100;} }

  • 字段的声明

  • 字段的初始化

         1.无显式初始化时,字段获得其类型的默认值,所以字段“永远都不会未被初始化”

         2.实例字段初始化的时机--------对象创建时

         3.静态字段初始化的时机--------类型被加载时

  • 只读字段

         1.实例只读字段

         2.静态只读字段

PS:静态构造器(static 类名)是在数据类型被运行环境加载的时候执行,而且只执行一次

class program
{
  static void main(string[] args)
  {
   console.writeline(Brush.DefaultColor.red);
   console.writeline(Brush.DefaultColor.green);
   

   /*Brush.DefaultColor = new Color(){red=255,Green=255};静态只读字段不能被赋值,只能进行初始化。静态只读字段初始化有两种方法,第一种静态构造器中初始化,第二种在声明的时候后面跟上初始化器*/
  }


  struct color
  {
   public int Red;
   public int Green;
   public int Blue;

  }
 
  class Brush
  {
   public static readonly Color DefaultColor = new Color(){red=0,green=0};//A
   /*==========声明了一个静态只读字段,并对它赋值。=================声明后对字段初始化======================================================*/ 



static Brush()//静态构造函数 //B { Brush.DefaultColor = new Color(){red=0,Green=0}; } /*A和B是一样的 } }

属性

  • 什么是属性

          1.属性是一种用于访问对象或类型的特征的成员,特征反映了状态

          2.属性是字段的自然扩展

              从命名上看,field更偏向于实例对象在内存中的布局,property更偏向于反映现实世界对象的特征

             对外:暴露数据,数据可以存储在字段里,也可以动态计算出

             对内:保护字段不被非法值“污染”

           3.属性是由get/set方法对进化来

  • 属性的声明

           1.完整声明-------后台成员变量与访问器

           2.简略声明-------只有访问器

           3.注意实例属性和静态属性

           4.只读属性,只有getter没有setter

  • 属性与字段的关系

          1.一般情况下,它们都用于表示实体(对象或类型的)状态

          2.属性大多数情况下是字段的包装器

          3.建议永远使用属性(而不是字段)来暴露数据,即字段永远都是private

using system;
namespace Property_Example
{
  class program
  {
   static void main(string[] args)
   {
    student stu1 = new student();
    stu1.setage(20);
    student stu2 = new student();
    stu2.setage(20);
    student stu3 = new student();
    stu3.setage(20);
    int avgage = (stu1.getage()+stu2.getage()+stu3.getage())/3;
    }
  }

  class student
  {
   private int age;
   public int Getage()
   { return this.age; }
   public void setage(int value)
   {
    if (value>=0&&value<=120){this.age=value;}
    else{ throw new Exception("age value is error");}

    } 

  }

}
/*-------------------------属性--------------------------------------*/


using system;
namespace Property_Example
{
  class program
  {
   static void main(string[] args)
   {
    student stu1 = new student();
    stu1.age(20);
    student stu2 = new student();
    stu2.age(20);
    student stu3 = new student();
    stu3.age(20);
    int avgage = (stu1.age()+stu2.age()+stu3.age())/3;
    }
  }

  class student
  {
   private int age;

   public int age
   {
    get
    {
     return this.age;
    }
     set
     {
      if (value>=0&&value<=120){this.age=value;}
      else { throw new exception("age value has error");}

      }

  }

}
using system;
namespace hello
{
  class program
  {
   static void main(string[] args)
   {
    student stu1 = new student(){id=1,name="a"};//用初始化器初始化一下
    console.writeline(stu.id);                  //stu.id=1
    }
  }

  class student
  {
   public int age{get;set;};
   public string name{get;set;}
  }

}

/*====================================================区别一======================================================================*/

using system;
namespace hello
{
  class program
  {
   static void main(string[] args)
   {
    student stu1 = new student(); //默认构造器对这边的实例对象的字段进行初始化
    console.writeline(stu.id); //stu.id=0
    }
  }

  class student
  {
   public int age{get;set;};
   public string name{get;set;}
  }

}

/*===================================================区别二实例化和初始化合并=============================================================*/

static void main(string[] args)
{
student stu = new student (2,"MR.OKAY");//传参数满足自定义构造器的需求
console.writeline(stu.id);
console.writeline(stu.name);
}

class student
{
public int id;
public string name;
public student ( int initid , string initname)
  { this.id      = intitid;
     this.name= initname;
   }
}





==== ================

集合初始化器:

用值来初始化数组:

int[] Array = new int[5] { 3, 11, 18, 22, 34 };

这是一种合并实例化和初始化数组的简捷方式。集合初始化器只是把这个语法扩展到集合上:

List<int> Collection = new List<int> { 8, 25, 33, 24, 20 };

通过合并对象和集合初始化器,就可以用简洁的代码配置集合了。下面的代码:

1 List<Student> student = new List<Student>();
2 
3 curries.Add(new Student("xiaoming", "1300108", 22));
4 
5 curries.Add(new Student("xiaohong", "1300109", 23));
6 
7 curries.Add(new Student("xiaozhang", "1300110", 22));

可以用如下代码替换:

 1 List<Student> student = new List<Student>
 2 
 3 {
 4 
 5 new Student1
 6 
 7 {Name = "xiaoming",ID = "1300108",Age=22},
 8 
 9 
10 new Student2
11 
12 {Name = "xiaohong",ID = "1300109",Age=22},
13 
14 new Student3
15 
16 {Name = "xiaozhang",ID = "1300110",Age=22},
17 
18 };
原文地址:https://www.cnblogs.com/zfcsharp/p/13715447.html