C#基础系列第三篇

前言:
完全是本身当上过程中著录之笔记,只不过分享一下给洋洋刚开头学习.net编程的人头能迅速的学会C#语言

  1. 做客修饰符(1)

(1)用当列方面

     1)public  表示以另地方都能让拜

     2)internal 表示只能以当下色中被聘

 (2)用当成员上面(字段方法等)

     1)public 可以以外地方被看

     2)private

     3)protected

     4)internal 表示只能当目前先后集全明白

     5)protected internal

 (3)图示说明5单访问修饰符的干C语言 1

  1. 访级别约束

(1) 子类的拜会级别不能够比较父类的赛

            class MyBase

            {

            }

            public class MySub:MyBase

            {

            }

            //这样描绘是误的

(2) 字段和性之顾级别不可知超越对许成员的访问级别

            class MyField

            {

            }

            public class MyClass

            {

                   MyField m;

                   public MyField M

                   {

                          get{return m;}

                          set{m=value;}

                   }

            }

            //这样写是谬误的

(3) 方法的走访级别不克超过参数与返回值的访级别

        class MyInternal

           {

           }

           public class MyClass

           {

                  public MyInternal Func(MyInternal f)

                  {

                  }

           }

           //这样写是张冠李戴的

 (4)”可访问性不雷同”的缪

 (5)不同意看不可看的积极分子 //写代码遵循这句话

  1. 静态方法

(1) 语法:

            public static 返回路 方法名(参数称为)

            {

                   //过程

            }

(2) 静态方法通过类名进行调用

             class MyStatic

            {

                   public static string GetString(string str)

                   {

                          return “$” + str + “$”;

                   }

            }

            class Program

            {

                   static void Main(string[] args)

                   {

                          string Name = MyStatic.GetString(“韩迎龙”);

                          Console.WriteLine(Name);

                   }

            }

     (3)静态方法,静态字段等还叫作静态成员

            1)静态成员属于类

            2)实例成员属于即目标

                class WindowsPhone

                   {

                          public static int num;

                          public WindowsPhone()

                          {

                                 num++;

                          }

                          string sim;

                          public string Sim

                          {

                                 get { return sim; }

                                 set { sim = value; }

                          }

                   }

                   class Program

                   {

                         
//静态属于持有此类的靶子,是看似的一个成员,而实例成员只有属即之实例

                          static void Main(string[] args)

                          {

                                 for (int i = 0; i < 100; i++)

                                 {

                                        new WindowsPhone();

                                 }

                                 WindowsPhone windows = new
WindowsPhone();

                                 windows.Sim = “232434343”;

                                 Console.WriteLine(windows.Sim);

                                 Console.WriteLine(WindowsPhone.num);

                          }

                   }

     (4)静态成员以及实例成员的以

            1)静态成员

                   ->在静态方法中,可以看静态成员

                   ->在静态方法中,不得以一直看实例成员

                   ->调用前初始化

            2)实例成员

                   ->在实例方法吃,可以一直看静态成员

                   ->在实例方法吃,可以直接访问实例成员

                   ->实例化对象时初始化

     (5)如果一个看似里的积极分子均是静态的,那么可以以这个类写静态的

            static class MyClass

            {

                   //成员

            }

     (6)静态类的积极分子必须还是静态的

            static class MyClass

            {

                   public static int GetNum()

                   {

                          return new Random().Next();

                   }

            }

            class Program

            {

                   static void Main(string[] args)

                   {

                          int num= MyClass.GetNum();

                          Console.WriteLine(num);

                   }

            }

  1. 静态成员

(1)
给字段的赋值都是出于构造方法完成的,即使写字段的当儿,对该展开初始化,也是有构造方法赋值的

     (2)如果调试的下,回先执行字段的初始化过程,在入到构造函数的。

            class MyClass

            {

                   public static int numStatic = 20;

                   //在构造方法里面赋值的

                   int num = 10;

                   public MyClass()

                   {

                         

                   }

            }

            class Program

            {

                   static void Main(string[] args)

                   {

                          new MyClass();

                   }

            }

            //断点调试先实施int num = 10;,在履行构造方法

     (3)静态字段的赋值也是由于静态构造方法完成的

            1)语法:

                   static 类名()

                   {

                          //赋值过程

                   }

            2)静态构造方法只能如此勾画

            3)静态构造方法不容许手动调用,由系统活动调用

            4)当次第一不良拜访该类的早晚(无论是静态还是非静态)

            5)静态构造方法只给调用一蹩脚

                   class MyClass

                   {

                          public static int numS;

                          static MyClass()

                          {

                                 numS = 10;

                          }

                          int numI;

                          public MyClass()

                          {

                                 numI = 20;

                          }

                   }

                   class Program

                   {

                          static void Main(string[] args)

                          {

                                 //第一种状态 访问实例成员

                                 new MyClass();

                                 new MyClass();

                                 //第二种  访问静态成员

                                 MyClass.numS = 40;

                          }

                   }

                   //在静态构造方法前面加断点执行

 (4)构造方法的默认值

           
1)构造方法就是为字段赋值用的,如果没有显得的供赋值,那么会有默认值

                   ->数值类  0

                   ->字符类型  /0

                   ->字符串类型 null

                   ->bool类型 false

 (5)静态成员只有在首先浅访的时光进行初始化,但是会一直保留在内存中

            1)对于静态字段容易导致内存的溢出起

                   class Program

                   {

                          static List<string> list = new
List<string>();

                          static void Main(string[] args)

                          {

                                 while (true)

                                 {

                                        list.Add(“123232435343”);

                                 }

                          }

                   }

  1. 静态类的接续问题

(1) 静态类不容许受接续,静态类只能继续自object

   C语言  (2)继承(多态)与静态是少单互斥的概念

     (3)静态类不克落实其他接口

     (4)sealed关键字

            1)密封类(密闭类),表示不克转继承

            2)string就是隐秘封类

            3)可以运用重写方法不再受另行写

     (5)静态类的本来面目

            1)查看IL代码 :abstract+sealed

            2)不允实例化 abstract

            3)不允许吃延续 sealed

     (6)举例说明:

            class MyClass1

            {

            }

            sealed class MyClass2

            {

            }

            class MySub : MyClass1

            {

            }

            class MySub2:MyClass2  //这样形容是荒唐的

            {    

            }