• 面向对象(委托)


    委托:
    也称为代理,事件也是一种委托;
    定义在类的最外面

    1、定义委托
    关键字:delegate
    函数签名:签名和函数保持一致
    定义委托的时候要根据函数来定义
    public delegate int First(int a,int b);
    指向的方法的返回类型,需要参数必须一致!

    2、定义委托变量,指向方法

    委托不能被实例化,因为不是类;

    First f = new JiaFa; //新建委托变量,指向方法,注意!!方法不需要小括号!!

    第二次可以使用+=

    public int JiaFa(int a,int b)
    {
    return a+b;
    }

    调用:
    f(5,3);

    可以理解为函数的指针,委托指向哪个函数,则这个委托就代表哪个函数
    可以让函数当做参数一样进行传递

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace ConsoleApplication1
      8 {
      9     //单列模式
     10     //控制一个类只能实例化一个对象
     11 
     12     //class Test
     13     //{
     14     //    public string name;
     15     //}
     16 
     17     //数据访问类
     18     class DBDA
     19     {
     20         public string host;
     21         public string database;
     22 
     23         //静态成员,用来存储该类的对象
     24         public static DBDA db = null;
     25 
     26         //让该类不能被实例化
     27         private DBDA()
     28         {
     29         }
     30 
     31         //提供一个造对象的方法,控制只能造一个对象
     32         public static DBDA DuiXiang()
     33         {
     34             if (db == null)
     35             {
     36                 db = new DBDA();
     37             }
     38 
     39             return db;
     40         }
     41     }
     42 
     43     //定义委托
     44     public delegate void SuiBian(string s);
     45 
     46     class Program
     47     {
     48         static void Main(string[] args)
     49         {
     50            // Test t1 = new Test();
     51            //Test t2 = new Test();
     52 
     53             //DBDA db = DBDA.DuiXiang();
     54             //db.host = "localhost";
     55             //DBDA db1 = DBDA.DuiXiang();
     56 
     57 
     58             //委托
     59             //把方法参数化
     60             SuiBian s = China;
     61 
     62             s += America; //+=是绑定方法  -=去掉一个方法
     63 
     64             //事件
     65             //事件就是一种特殊的委托
     66 
     67 
     68             //调用语音方法
     69             Speak(s);
     70 
     71            
     72 
     73             Console.WriteLine();
     74             Console.ReadLine();
     75 
     76 
     77             //面向对象三大特性
     78             //设计模式
     79         }
     80 
     81         //语音功能的方法
     82         static void Speak(SuiBian yu)
     83         {
     84             yu("张三");
     85 
     86             //if (n == 0)
     87             //{
     88             //    America();
     89             //}
     90             //else if (n == 1)
     91             //{
     92             //    China();
     93             //}
     94             //else if (n == 2)
     95             //{
     96             //    HanYu();
     97             //}
     98     
     99         }
    100 
    101         //语音包
    102         public static void America(string s)
    103         {
    104             Console.WriteLine(s+"hello");
    105         }
    106         public static void China(string s)
    107         {
    108             Console.WriteLine(s+"你好");
    109         }
    110         public static void HanYu(string s)
    111         {
    112             Console.WriteLine(s+"bjdkaj");
    113         }
    114 
    115   
    116     }
    117 }
  • 相关阅读:
    go,指针
    go ,字符串的编码及如何统计字符长度,
    go中的map,如python中的字典
    go 可变长参数的应用
    go 数组切片
    Django 模板层
    Django的路由层和视图层
    Django
    Django简介
    JQurey
  • 原文地址:https://www.cnblogs.com/bloodPhoenix/p/5764451.html
Copyright © 2020-2023  润新知