zoukankan      html  css  js  c++  java
  • 类的设计

    构造函数 constructor

    函数名与类名总是相同;没有返回值。

    public class BankCustomer
    
     { 
    
    public BankCustomer(string fn, string In, decimal bal)
    
    {
    
     this.FirstName=fn; this.LastName=In;this.Balance=bal;
    
     } 
    
    }
    
    BankCustomer c;
    
    c = new BankCustomer();
    
    c = new BankCustomer(“kim”,”lee”,1000.0m);

    通过重载,构造函数可以任意多。

    public class BankCustomer
    
    {
    
       public BankCustomer(string fn, string In, decimal bal)
    
         {
    
            this.FirstName=fn; this.LastName=In;this.Balance=bal;
    
          }
    
        public BankCustomer()
    
         {
    
            this.FirstName=””; this.LastName=””;this.Balance=0.0m;
    
          }
    
         public BankCustomer(string fn, string In)
    
         {
    
            this.FirstName=fn; this.LastName=In;this.Balance=0.0m;
    
          }
    
         public BankCustomer(System.I0.StreamReader reader)
    
         {
    
            this.FirstName=reader.ReadLine;
    
            this.LastName=reader.ReadLine();
    
            this.Balance=decimal.parse(reader.ReadLine());
    
          }
    
    }

    构造函数间的调用

    public class BankCustomer
    
    {
    
            public BankCustomer(string fn, string In, decimal bal)
    
           {
    
             this.FirstName = fn;
    
             this.LastName = In;
    
             this.Balance = bal;
    
           }
    
           public BankCustomer( string fn, string In)
    : this (fn, In, 0.0m)
    
           {  }
    
           public BankCustomer(System.I0.StreamReader reader)
    
    : this (reader.ReadLine(), reader.ReadLine(), decimal.Parse(reader.ReadLine()))
    
            {  }
    
    }
    小结:

    构造函数:类中特殊的方法,多用于初始化实例的数据成员,在实例化new时被自动调用。

    1.函数名与类名总是相同;

    2.没有返回值;

    3.任意数量(通过重载)

    4.构造函数间的调用

    5.访问修饰符的作用 (public   protected  private)

    如果没有显示定义,那么系统提供一个不带任何参数的public的构造函数

    静态构造函数

    属性property

    内部看像函数,外部看像字段

    读/写 有get/set存取器来进行

    public class BankCustomer
    
    {
    
    …
    
    private decimal m_Balance;
    
    public decimal Balance
    
    {
    
    get {return this.m_Balance;}
    
    set
    
    {
    
       if (value < 0.0m)
    
        ……
    
        else 
    
        this.m_Balance = value
    
    }
    
    }
    
    }

    索引器  Indexer

    索引器是一类特殊的属性,通过他们你就可以像处理数组一样处理对象。

    索引器通常在代表元素集合的类中定义

    public class BankCustomer
    
    {
    
           private double balance;
    
          private Person[] relatives;
    
          public BackCustomer()
    
          {
    
                relatives = new Person[100]
    
          }
    
    }

    属性名是this,意思是回引类的当前实例,参数列表包含在方括号而非括号内

    public Person this [int index]
    
    {
    
         get
    
         {
    
              return relatives [index];
    
         }
    
         set
    
         {
    
             if(value != null)
    
              {
    
                return value;
    
               }
    
         }
    
    }
    BankCustomer bc = new BankCustomer();
    bc[10] = new Person(“Name”);

    委托 delegate***

    想象成C++中的函数指针,但不同点在于delegate完全面向对象的-----既封装方法,又封装对象实例

    定义委托实际上是定义一个类型的委托,不是一个具体的实例

    委托类型指定它代表的方法的返回类型参数表

    它代表具有相同参数列表和返回类型的任何方法

    委托的声明方式和方法的声明相似,包括的是delegate关键字

    声明中必须包括委托所表示的方法的返回类型和参数列表

    public delegate bool ProcessAnything(double d);

    创建委托实例-----new 关键字

    ProcessAnything pa = new ProcessAnything(account.Withdraw);

    括号里面是实例方法,此方法必须和代理声明时代返回值类型和参数列表相同。

    委托的调用:

    委托的调用时通过输入委托实例的名称和要传递给委托所表示的方法的参数

    public delegate double DelegateCompute(double x);

    public class MoneyCompute
    
    {
    
           public static double compute(double t, DelegateCompute dc)
    
            {
    
                return dc(t);
    
            }
    
    }

    多重委托----引用多个方法的委托,它连续调用每个方法

    为了把委托的单个实例合并为一个多重委托,委托必须是同类型的,返回类型必须是void,不能带输出参数(可以带引用参数)

    多重委托应用于事件模型中

    牛牛的解释:

           //delegate委托 实际上是一个类,其实它的作用 是配合事件一起比较多 就是event
           //首先 委托的定义
           //委托 需要定义 方法返回类型 这里是void
           //以及参数这里是str 但是可以根据业务需要定义
           //委托实际就像中介一样 所以需要定义跟委托一样方法返回类型以及参数一样的方法
           delegate void ProcessString(string str);
           static void Main(string[] args)
           {
               ProcessString _processStr = DemoProcessStr;//这里相当于委托的实例化 
              
               //意思 讲方法委托上去 或者就是讲将方法指向这个委托 
               //那么 怎么输入已经委托方法里面东西了?
               //既然将方法指向一个委托了,那么不需要继续关心方法调用 直接调用委托即可
               _processStr("HuXiaoxia");
               _processStr += DemoProcessStr2;         
               _processStr("对哒");
    
               Console.ReadLine();
    
           }
           static void DemoProcessStr(string str)
           {
               Console.WriteLine(string.Format("{0} 是好妞.", str));
           }
           static void DemoProcessStr2(string str)
           {
               Console.WriteLine(string.Format("对不?{0}.", str));
           }

    事件 event

    C#中使用委托模型来实现事件

    事件处理方法不必在将生成事件的类中定义

    需要做的事情就是把事件源和事件处理程序结合起来

    使用事件处理委托,简称事件委托,可以定义为生成事件的类的一个成员。

    事件委托是多重的

    事件委托的形式:

    public delegate void MouseEventHandler(object source,EventArgse);

    object source:事件源

    EventArgse:System.EventArgs类的实例或派生类实例,它包含事件的另外的信息

    用户自定义事件委托

    public delegate void NameEventHandler(object source,NewEventArgse);

    必须自己来定义NameEventArgs类

    创建事件委托的实例:不使用关键字new,使用event关键字

    public event NameEventHandler handler;

    触发事件

    1.把事件委托的一个实例定义为类的成员

    2.确定何时生成事件代码

    3.定义生成提供事件的EventArgs对象的代码

  • 相关阅读:
    1582. Special Positions in a Binary Matrix
    623. Add One Row to Tree
    617. Merge Two Binary Trees
    643. Maximum Average Subarray I
    645. Set Mismatch
    1022. Sum of Root To Leaf Binary Numbers
    835. Image Overlap
    HDU Marriage is Stable (稳定婚姻匹配)
    HDU Stable Match (稳定婚姻匹配)
    HDU Maximum Clique (最大团)
  • 原文地址:https://www.cnblogs.com/HuXiaoxia/p/3402680.html
Copyright © 2011-2022 走看看