读书笔记2

来源:互联网 发布:室内设计网络教育 编辑:程序博客网 时间:2024/05/18 00:02

第四章:深入组件

1、数组(array):一种数据结构,用于存储多个变量,有一个或多个相关下标;根据和数组下标量的多少,可以将数组分为一维数组和多为数组

2、一维数组(single-dimensional array):的元素具有一个相关的下标

        <数据类型>[]<数组1>=new <数组类型>[]<大小>

3、多为数组(multidimensional array):的元素具有多个相关的下标

       有两种类型:矩形数组、正交数组或锯齿数组

     ①、矩形数组(rectangular array):的各行所包含的列数相同

          具有两个相关下标的数组叫二维数组

         int [,] integer={{2,3},{3,4},{4,5}};

           具有三个相关下标的数组叫三维数组

         int [,,] integer={{1,2,3},{2,3,4},{3,4,5}};

           for循环初始化数组

          int [,] Integer=new int[5,10];

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

          {

             for(int j=0;j<10;j++)

             {

                 Integer[i,j]=i*j;

}

}

     ②、正交(orthogonal)或锯齿(jagged)数组:每行具有不同的列数

          int [][] integer=new int[2][];

          integer[0]=new int[5];//第一行包含5

          integer[1]=new int[10]; //第二行包含10

4、数组中的方法:

    ①.Length:确定数组的大小或者数组中的元素数量

            int i=integer.Length;

    ②、GetLength():确定多为数组的大小

           int i=integer.GetLength(1);//确定该数组的第二维的大小

    ③、Reverse():颠倒数组元素的顺序,是一个静态方法

           array. Reverse(integer);

    ④、Sort():对数组元素排序,升序排列

           array. Sort(integer);

        int[] Marks={90,70,80,60,95,68};

        int i=Marks.Length;

        array.Sort(Marks);

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

        {

         Console.WriteLine(j);

}

5、集合(collection):是一组对象

   foreach(string str1 in collection)

   {console.writeline(str1);}

   ①、创建集合:System.Collections.IEnumerable接口包含了所有的集合;它有GetEnumerator()方法,这个方法返回类型Enumerator的对象。Enumerator对象只能从集合内读取数据,而不能修改集合;实例化IEnumerable需调用MoveNext()方法,这个方法用于集合元素之间的移动;Current特性读取Enumerator对象所引用的值,仅返回对集合元素的引用,因此为了获得元素的实际值,可以将引用类型转换为元素类型

        public interface IEnumerable

{

   IEnumerator GetEnumerator();

}

public interface IEnumerator

{

   bool MoveNext();

   object Current//Enumerator对象所引用的值,仅返回对集合元素的引用

   {

      Get;

}

void Reset();//重置Enumerator对象的值

}

       ②、使用集合:

            集合所使用的接口:命名空间都是System.Collections

            ICollection接口:用于规定枚举元素、大小和方法,以保持集合的同步

            IDictonary接口:用于表示相关键和值的集合

            IList接口:用于表示对象集合,可以为这个对象创建单独的索引

            ICloneable接口:用于创建类的新接口,用与现有实例类似的值创建新实例,这便是克隆

        ③、ArrayList类:用于创建动态增长的数组,实现了接口IList

             using System.Collections;

             public class ArrayList1

             {

                public static void Main()

                {

                    ArrayList list1=new ArrayList();

                    List1.Add(“this”);

                    List1.Add(“is”);

                    List1.Add(“a”);

                    List1.Add(“sample”);

                    List1.Add(“ArrayList”);

}

}

         ④、与集合一起使用的接口中的方法:

             ICollection接口:

                CopyTo():将ICollection接口中的元素复制到指定的数组中,也可以指定开始复制元素的索引编号

             IDictonary接口

                 Add():在IDictonary接口中添加一个元素,可以指定添加的元素的索引和值

                 Remove():在IDictonary接口中删除一个元素,需指定要删除元素的索引

                 Clear():删除IDictonary接口中的所有元素

                 GetEnumerator():为IDictonary接口返回IDictonaryEnumeratkor对象

                 Contains():用于查找IDictonary接口中的特定元素,需指定索引

             IList接口:

                 Add():在IList接口中添加元素

                 Remove():从IList接口中删除第一次出现的某个对象

                 RemoveAt():从IList接口删除指定索引的元素

                 Clear():删除IList接口中的所有元素

                 Insert():在IList接口中在指定索引处插入一个元素

                 IndexOf():查找指定元素的索引值

             IConeable()接口:

                 Clone():创建现有类实例的复制品

6、索引:

     <修饰符><类型>this<参数列表>

     this:作为索引的名称,索引没有必要具有明确的名称

     public  int  this [int x]

       //下面这段代码用于读取索引和向索引中写入数据

      class Class1

      {

         int  Variable1,Variable2;

         public int this [int x]

         set

         {

            switch(x)

                {

                      case 0:

                          Variable1=10;

                      break;

                      case 1:

                          Variable2=20;

                      break;

}

}

get

{

    switch(x)

    {

       case 0:

           return Variable1;

       break;

       case 1:

           return Variable2;

       break;

}

}

}

7、装箱(boxing)和拆箱(unboxing

   (1)、装箱:是一种数据转换技术,隐含地将值类型转换为对象类型或引用类型

        class  Class1

        {

           public static void Main()

           {

                string str1=”New String”;

                object obj1=str1;

                Console.WriteLine(obj1);

}

}

   (2)、拆箱:是一种数据转换技术,和装箱相反,显式地将对象类型转换成值类型

       string str1=”New String”;

       object obj1=str1;

       string str2=(string)obj1;

8、预处理指令(preprocessor directives):不会执行的命令

1)、声明预处理指令要用#

2)、#region#endregion定义一组语句作为整体执行

3)、#defineundef:分别用于定义和删除符号的定义

      #define symbol //定义符号symbol

      #undef  symbol //删除符号symbol

4)、#if #endif #else #elif:对某段代码进行条件编译

     #define symbol //如果没有这个定义则执行

     class Class1

     {

         #if symbol

            Console.WriteLine(“symbol exists”);

         #else

            Console.WriteLine(“symbol does not exsits”);

         #endif

}

5)、#error#waring:前者产生错误,停止执行代码,后者则通过显示#waring语句中的文本,而向程序员发出警告,继续编译代码

     #define symbol //如果没有这个定义则执行

     using System;

     class Class1

     {

         #if symbol

            Console.WriteLine(“symbol exists”);

         #else

            #waring symbol is not defined

         #endif

}

 

第五章:属性和特性

1、属性(attributes):用于存储方法和类的其他信息

  1)、声明属性:[属性名称(属性参数)]

2、属性类:存储用户定义属性

  1)、单用途属性类:不能将类中声明的属性赋予多个相同实体

       [Color(“Green”)]

       class Car

       {…}

  2)、多用途属性类:可以将类中声明的属性赋予多个相同的实体

       [Color(“Green”),Color(“Blue”)]

       class Car

       {…}

3、属性参数:

  1)、位置参数(positional parameters):在属性类的public析构函数中声明的参数,包含参数表达式,须与参数一起使用

  2)、名字参数(named parameters):在属性类的non-staticpublic读写字段或属性类的特性中声明的参数,用于读取属性值和向属性中写入值

4、默认属性:

  1)、Obsolete属性:用于标记在任何程序代码中都不再继续使用的元素

       [Obsolete (“Do not use this method in the code”),true]

        里面如果是true:产生错误,并停止执行程序

            如果是false:仅产生警告

  2)、Conditional属性:用于标记为conditional属性的一组语句进行条件编译

        [Condition (“symbol”)]

        public void Method1  //只有在定义了symbol的情况下才可以调用Methods

        {}

  3)、AttributeUsage属性:和属性类一起使用,这个属性的参数,存储有关属性类的信息

       [AttributeUsage (AttributeTargets.Class|AttributeTargets.Struct,Allow Multiple=true,Inherited=true)]

        public class Attribute1:Attribute

        {}

//第一个参数定义了为之声明属性的实体列表,第二个参数如果设置为false则是单用途属性类,如果为true则为多用途属性类,第三个参数用于定义导出类能否使用这个属性

5、特性(property):存储了关于对象的信息

       1)、声明特性

            <修饰符><类型><特性名称>

            {}

       2)、访问器

            Get访问器:用于读取特性值的方法

            Set访问器:向特性中写入值的方法

           public class Car

           {

               string color;

               public string Color1

               {

                   get

                    {

return color;

}

set

{

      color=value;

}

}

}

      3)、只读特性:只有get访问器

            只写特性:只有set访问器

            读写特性:有get访问器和set访问器

      4)、特性类型:

             静态类型(static  property):用static修饰符声明的特性。具体类的实例不能应用静态特性。

             实例特性(instance  property):没有static修饰符声明的特性。具体类的实例可以引用实例特性,也叫非静态特性(non-static  property)。

 

 

 

 

 

第六章:线程

    1、线程(thread)基础:线程是程序的基本执行单位;是系统分配处理器时间的最小单位。

    2、使用方法:

        1)、需要大量时间的操作:创建两个线程,工作线程(worker thread)和用户线程(user thread

               工作线程:执行需要大量时间的操作

               用户线程:管理用户交互

        2)、网上传递数据:需要执行完成多个操作的应用程序。如更新数据库,要有用户线程和工作线程,用户输入数据,工作线程更新数据库中的数据。

        3)、使用过多的线程会降低程序的性能

        4)、抢占式多任务(preemptive  multitasking):用指定的时间片(time  slice)执行多线程的过程。

    3、创建线程:

         using System.Threading;

         class Class1

{

public  void Method1()

{

   Consonle.WriteLine(“Method1 is the starting point of execution of the thread”);

}

public static void Main()

{

   Class1 newClass=new Class1();

   Thread thread1=new Thread(new ThreadStart(newClass.Method1));

}

}

 

调用线程:

Thread1.Start();//只有调用了Start()方法,线程才会启动

4、定义一个工作线程:

      Thread  Thread1=new Thread(new ThreadStart(newClass.Method1));

      Thread.Nme=”Update Records Thread”;//给线程定义名称

5、特性:

     1)、IsAlive特性:用于确定线程的执行过程是否已经结束或者还处于执行状态,正在执行放回true,否则返回false

     2)、ThreadState特性:指出了线程的执行状态。

6、终止线程:

     Thread1.Abort();//调用Abort(),线程并不会马上停止,程序停止try模块的时候得先执行finally模块。

7合并线程:

     Thread1.Join();

  Join()通常和Abort()一起使用,当调用完Abort()后就调用Join(),以终止线程

8、挂起线程:

     Thread1.Suspend();//得等到线程到达安全点停止

     Suspend()不会永久杀死线程,只是让线程停止一会儿,然后继续执行,当需要继续执行线程时得调用Resume()方法,这个方法是让线程从挂起点开始启动

     Thread1.Resume();

9、线程休眠:

     Thread1.Sleep(2000); //可以马上停止线程

例子:

using System.Threading;

         class Class1

{

public  void Method1()

{

   Consonle.WriteLine(“Method1 is the starting point of execution of the thread”);

}

public static void Main()

{

   Class1 newClass=new Class1();

   Thread thread1=new Thread(new ThreadStart(newClass.Method1));

         Thread.Nme=”Sample Thread”;//给线程定义名称

   Thread1.Start();

   Consonle.WriteLine(“the sample execution of the thread is started”);

   Threadq.Abort();

}

}

 10、线程的状态

   Thread1.Start();//当调用该方法,线程状态转为Running

Thread1.Sleep(2000); //当调用该方法时,线程状态转变为WaitSleepJion.这暗示着,在指定的时间内不会运行线程。

     Thread1.Abort();//当从另一个线程中调用Abort()时,线程状态转变为AbortRequested,然而当执行Abort方法时,线程状态转变为Abort

     Thread1.Suspend();//当从另一个线程中调用该方法时,线程状态转变为SuspendRequested, ,然而当执行Suspend方法时,线程状态转变为Suspend

   Thread1.Resume();//让线程从挂起点开始启动,状态为Running

       

11、线程优先级:

   1)、Highest:首先执行具有Highest级别的线程。在执行具有Highest级别线程之前,它停止运行所有其他线程

   2)、AboveNormal:具有AboveNormal级别的线程,在具有Highest级别的线程之后执行,但在所以其他级别的线程之前执行。

3)、Normal:既有Normal级别的线程是是优先级列表中第三个级别,将根据抢占式多任务过程为这个线程分配时间片。

4)、BelowNormalLowest:具有BelowNormalLowest级别的线程仅仅在系统没有找到其他优先级别的线程的情况下才执行的。只有不重要的线程才会赋予这个级别

5)、所有优先级都是ThreadPrioriryEnumeration枚举的一部分,所有的优先级别都是相对一个进程来说的。

    Thread Thread1=new Thread(new ThreadStart(newClass.Method1));

         Thread1.Prirority=ThreadPrirority.Highest;

     12、同步(synchronization):帮助同步使用系统上正在运行的多线程所访问的变量和对象,换句话说就是,同步确保在任何时候一个线程只能访问某个变量。

       1)、对对象加锁时,任何其他线程都不能访问这个对象。任何需要访问这个对象的其他线程都必须等待另一个对象释放锁定。

       2)、加锁和释放对象锁定需要大量资源,而且增加了应用程序的运行成本

       3)、在同步对象时,可能出现的另一个情况是死锁(deadlocking)。

               两个不同线程同时锁定了两个不同的对象,当其中一个线程要访问另一个线程锁定的对象的时候就会出现死锁。

 

 

原创粉丝点击