zoukankan      html  css  js  c++  java
  • C#面向对象基础(八) 代理

    代理/委托

     代码下载

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApp
    {
        
    //代理 / 委托    方法的指针  C  事件的基础   方法 作为 参数
        
    // 定义一个代理 (不同的“代理”,代理不同的方法.)
        delegate void MyDelegate ();
        
    //       类型 /代理名称 /方法的参数表
        
    // MyDelegate  是一种类型   class MyDelegate
        class Program
        {       
            
    static void Main(string[] args)
            {
                MyDelegate d 
    = new MyDelegate(StaticMethod);
                
    //d  是  StaticMethod的代理
                
    // StaticMethod();
                d();
                Program p 
    = new Program();
                d 
    = new MyDelegate(p.InstanceMethod);
                d();

                
    //d = new MyDelegate(SayHello); //错误,代理与方法不匹配

                Console.Read();         
            }
            
    static void StaticMethod()
            {
                Console.WriteLine(
    "某静态的方法");
            }
            
    void InstanceMethod()
            {
                Console.WriteLine(
    "这是一个实例的方法");
            }

            
    static void SayHello(string name)
            {
                Console.WriteLine(
    "hello , {0}",name);
            }

        }      
    }
  • 相关阅读:
    Python编程四大神兽:迭代器、生成器、闭包和装饰器
    Linux基础
    3.8记录
    3.7记录
    3.6进度记录
    3.5进度
    3.4进度
    3.3进度
    3.2进度记录
    3.1记录
  • 原文地址:https://www.cnblogs.com/imxh/p/2177572.html
Copyright © 2011-2022 走看看