.NET学习(4) C#中类的设计

来源:互联网 发布:mysql该不该用join 编辑:程序博客网 时间:2024/06/06 01:30

 

 看了Modern C#系列课程4--C#中类的设计的笔记,主讲 俞晖

        *********************************************************

Property(属性):看着像方法,但是可以像字段一样直接使用。

Public class BankCustomer

{

Private decimal m_Balance; //私有field

Public decimal Balance   //property

{

    Get{ return this.m_Balance; }

    Set{

        If(value < 0.0m)  //value是系统自定义的输入参数

            ;

        Else

            This.m_Balance = value;

}

}

调用:bc = new BankCustomer();

bc.Balance = bc.Balance – 10.0m;//其实是对m_Balance操作

 

****************************************************************

索引器 :其实也是属性的一种,是对类似于数组的操作。

Public class BankCustomer{

Private Person[] relatives;//定义一个索引器

Public BankCustomer(){

 

        Relatives = new Person[100];//对100个Person对象索引

 

}

Public Person this[int index]{//这个方法是主要的,建立索引

    Get{

        Return relatives[index];

        }

    Set{

        If(value != null)

            Relatives[index] = value;

}

}

调用:bc = new BankCustomer();

Bc[10] = new Person(“hello”);

TextBox1.Text = bc[10].name;

//person是一个类

  public class Person

  {

                private string name;

                public Person()

                {

                }

                public Person(string name)

                {

                              this.name = name;

                }

                public string Name

                {

                              get

                              {

                                            return this.name;

                              }

                }

  }

***********************************************************

 

委托代理delegate相当于函数指针。指向参数表和返回类型一致的所有函数。这是一个类

MoneyCompute.cs

using System;

public delegate double DelegateCompute(double x);//定义代理,匹配参数和返回值都是double的方法

namespace WindowsApplication3
            {
                public class MoneyCompute
                {

        public static double Compute(double t, DelegateCompute dc)//代理的方法在这里被调用
                    {
                       return dc(t);
                    }

        public MoneyCompute()//构造函数
                   {  }
                }
             }

 

Form1.cs

public static double Low(double T) //被代理的方法
            {
                 return 0.02*T;
            }

public static double High(double T) //被代理的方法
           {
                 return 0.1*T;
            }

private void button1_Click(object sender, System.EventArgs e)
            {
                 double result;
                 DelegateCompute dc; //因为代理是一个类,所以要生成类的实例
                 dc= new DelegateCompute(Form1.Low);//代理Form1.Low方法。参数和返回值和代理函数一致
                 result = MoneyCompute.Compute(1000.0,dc);//调用代理函数
                 this.low.Text = result.ToString();


   dc = new DelegateCompute(Form1.High);
   result = MoneyCompute.Compute(2000.0,dc);
   this.high.Text = result.ToString();

}

 

 代理还可以同时顺序调用多个被代理方法,但是这些被代理方法的返回值必须为空。

 Messages.cs

 using System;

public delegate void Message();//定义代理,返回值为void

namespace WindowsApplication4
            {
                   public class Messages
                   {
                           public Messages()
                           {  }
  
                           public static void Greeting() //被代理方法
                            {
                                  Console.WriteLine("Welcome to Webcast and MSDN China");
                              }

                public static void DateAndTime()//被代理方法
                             {
                                   Console.WriteLine(DateTime.Now.ToLongDateString());
                              }

                 public static void Feedback()//被代理方法
                              {
                                    Console.WriteLine("Your feedback are very important for us");
                               }
                  }
           }

          Form1.cs

          Message msg;

           private void View_Click(object sender, System.EventArgs e)
          {
                 msg = new Message(Messages.Greeting); //因为是类,所以要生成实例
                 msg += new Message(Messages.DateAndTime); //增加一个事件

                 Message msg2 = new Message(Messages.Feedback);
                 msg += msg2;
                 msg();//运行代理方法
            }

     *********************************************************************

     事件处理event,其实也用到了代理的知识

     NameList.cs

     using System;
     using System.Collections;

     namespace WindowsApplication5
    {
         public class NameList
         {
             ArrayList list;
             public event NameListEventHandler nameListEvent;//生成事件实例,注意用的event关键字

             public NameList()
            {
                 list = new ArrayList();
            }

            public void Add(string name)
            {
                list.Add(name);
                if(nameListEvent != null)//是否有事件处理
                {
                       nameListEvent(this, new NameListEventArgs(name, list.Count));//调用事件处理
                }
            }

         }
     }

      NameListEventArgs.cs

      using System;

      public delegate void NameListEventHandler(object source, //事件处理代理

                                                                           WindowsApplication5.NameListEventArgs  args);

      namespace WindowsApplication5
      {
          public class NameListEventArgs : EventArgs
          {
               string name;
               int count;

               public NameListEventArgs()
               {  }

               public NameListEventArgs(string name, int count)
               {
                       this.name = name;
                       this.count = count;
               }

               public string Name
               {
                   get{    return name;   }
                }

                public int Count
                {
                    get{    return count;}
                }
            }
        }
         Form1.cs

         private void button1_Click(object sender, System.EventArgs e)
         {
                NameList names =new NameList();
                names.nameListEvent += new NameListEventHandler(NewName);//代理NewName方法为事件处理方法
                names.nameListEvent += new NameListEventHandler(CurrentCount);//代理CurrentCount方法为事件处理方法

                names.Add("MSDN");//每add一次,都要调用NewName,CurrentCount方法
                names.Add("Webcast");
         }

         public static void NewName(object source, NameListEventArgs args)//被代理的事件方法
         {
               Console.WriteLine(args.Name + "was added to the list");
         }

         public static void CurrentCount(object source, NameListEventArgs args)//被代理的事件方法
         {
              Console.WriteLine("list currently has " + args.Count + " items.");
         }

原创粉丝点击