接口、委托、特性、反射、多播委托

接口

用于与服务有关,只有方法声明,无方法体,接口不能放字段。

默认访问修饰符为public

继承了接口,必须实现所有的接口成员

接口也可以多重接口

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

namespace _01接口
{
    class Program:ICalc,IWork
    {
        static void Main(string[] args)
        {

        }

        public int add(int a, int b)
        {
            return a + b;

        }

        public int cheng(int a, int b)
        {
            return a * b;
        }

        public void Run()
        {
            throw new NotImplementedException();
        }

        public void Talk()
        {
            throw new NotImplementedException();
        }
    }
    class People : ICalc
    {
        public int add(int a, int b)
        {
            throw new NotImplementedException();
        }

        public int cheng(int a, int b)
        {
            throw new NotImplementedException();
        }

        public void Run()
        {
            throw new NotImplementedException();
        }

        public void Talk()
        {
            throw new NotImplementedException();
        }
    }
}

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

namespace _01接口
{
    interface IWork
    {
        void Run();
        void Talk();
    }
}

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

namespace _01接口
{
    interface ICalc:IWork
    {
        int add(int a, int b);
        int cheng(int a, int b);
    }
}

委托

将方法作为方法的参数代入另一个方法中,可以理解为一种数据类型,直接赋值给一个方法。

两种委托:

Action:可有可无参数,无返回值;

Function:必须有一个参数,有返回值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace _02委托
{
    class Program
    {
        //delegate string DelTalk();
        delegate int DelAdd(int a,int b);
        static void Main(string[] args)
        {
            //Console.WriteLine("进来一个人");
            Console.WriteLine("请进行加法运算");
            People p = new People();
            //SetName(p.Talk); 
            NumAdd(p.Add);
            Console.ReadLine();
        }
        //static void SetName(DelTalk talk)
        //{
        //    Console.WriteLine("这个人的名字是:"+talk());
        //}
        static void NumAdd(DelAdd add)
        {
            Console.WriteLine("这两个数之和是:"+add(2,3));
        }
    }
}

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

namespace _02委托
{
    class People
    {
        
        //public string Talk()
        //{
        //    return "张三";
        //}
        public int Add(int a,int b)
        {
            return a + b;
        }

        
    }
}

特性(Attribute)

特性是用于在运行时传递程序中各种元素的行为信息的声明性的标签。一个声明性标签是通过放置在它所应用的元素前面的方括号([]来描述的。

预定义特性:

AttributeUsage、Conditional、Obsolete(过时的)

Conditional代码:

#define DEBUG
using System;
using System.Diagnostics;
public class Myclass
{
    [Conditional("DEBUG")]
    public static void Message(string msg)
    {
        Console.WriteLine(msg);
    }
}
class Test
{
    static void function1()
    {
        Myclass.Message("In Function 1.");
        function2();
    }
    static void function2()
    {
        Myclass.Message("In Function 2.");
    }
    public static void Main()
    {
        Myclass.Message("In Main function.");
        function1();
        Console.ReadKey();
    }
}
//Obsolete(过时的)
using System;
public class MyClass
{
   [Obsolete("Don't use OldMethod, use NewMethod instead", true)]
   static void OldMethod()
   { 
      Console.WriteLine("It is the old method");
   }
   static void NewMethod()
   { 
      Console.WriteLine("It is the new method"); 
   }
   public static void Main()
   {
      OldMethod();
   }
}

反射(Reflection)

反射指程序可以访问、检测和修改它本身状态或行为的一种能力。

//实例类
class ReflectionClass
{
    public int id;

    private string name;
    /// <summary>
    /// 姓名
    /// </summary>
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private string age;
    /// <summary>
    /// 年龄
    /// </summary>
    public string Age
    {
        get { return age; }
        set { age = value; }
    }
    private string sex;
    /// <summary>
    /// 性别
    /// </summary>
    public string Sex
    {
        get { return sex; }
        set { sex = value; }
    }

    public ReflectionClass(string name, string age)
    {
        this.name = name;
        this.age = age;
    }
    public ReflectionClass(string sex)
    {
        this.sex = sex;
    }
    public ReflectionClass()
    { }

    public void Show()
    {
        Console.WriteLine("姓名:" + name + "\n" + "年龄:" + age + "\n" + "性别:" + sex);
    }
}

多播委托

每个委托都只包含一个方法调用,调用委托的次数与调用方法的次数相同。如果调用多个方法,就需要多次显示调用这个委托。当然委托也可以包含多个方法,这种委托称为多播委托。

/// <summary>

/// 多播委托

/// </summary>

public class MultiDelegate

{

private delegate int DemoMultiDelegate(out int x);

private static int Show1(out int x)

{

x = 1;

Console.WriteLine("This is the first show method:"+x);

return x;

}

private static int Show2(out int x)

{

x = 2;

Console.WriteLine("This is the second show method:"+x);

return x;

}

private static int Show3(out int x)

{

x = 3;

Console.WriteLine("This is the third show method:"+x);

return x;

}

/// <summary>

/// 调用多播委托

/// </summary>

public void Show()

{

DemoMultiDelegate dmd = new DemoMultiDelegate(Show1);

dmd += new DemoMultiDelegate(Show2);

dmd += new DemoMultiDelegate(Show3);//检查结果

int x = 5;

int y= dmd(out x);

Console.WriteLine(y);

   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值