C#学习之路抽象类和委托

抽象类(abstract)


抽象类不能实例化,类是一个模板,抽象类就是半个模板。当我们继承抽象类的时候必须实现抽象函数的方法
God.cs

abstract class God
    {
        public abstract void Fly();
    }


Human.cs//继承抽象类必须实现抽象函数的方法

class Human:God
    {
        public override void Fly()
        {
            Console.WriteLine("神会飞");
        }
    }

委托

C#中的委托(delegate)类似于C或C++中函数的指针,委托对象必须使用 new 关键字来创建,且与一个特定的方法有关。委托特别用于实现事件和回调方法。

Program.cs

Human human = new Human();//子类构造函数,调用重写方法
            human.Fly();
//也可以这样,父类(God)声明一个神,使用子类(Human)实例化
God god = new Human();

            god.Fly();

//委托可以把方法当做参数来传递
private delegate string Get(); //定义一个委托
static void Main(string[] args)
{
    int x=40;
    //1.
    Get get=new Get(x.ToString);//委托当成参数
    string a=get();//可以和方法一样直接使用
    //2.Get a=x.ToString;
}


private delegate void PrintString();//声明一个委托类型
        static void PrintStr(PrintString print)//传递一个委托类型的参数
        {
            print();
        }
        static void Method1()
        {
            Console.WriteLine("Method1");
        }
        static void Method2()
        {
            Console.WriteLine("Method2");
        }
static void Main(string[] args)
{
            PrintString method = Method1;//委托类型也必须要初始化才能使用
            PrintStr(method);
            method = Method2;
            PrintStr(method);
}

Action委托:系统内置(预定义)的一个委托类型,他可以指向一个没有返回值没有参数的方法 

Func委托: 可以传入参数,必须有返回值 

委托的多播(Multicasting of a Delegate)

“+” 运算符:可以进行合并委托对象,一个合并委托调用它所合并的两个委托。

“-”  运算符:用于从合并的委托中移除组件委托。

当创建一个委托被调用时要调用的方法的调用列表时称为委托的多播,又名组播。举例来说就是本来分开运算的两个对象,合并到一起,同时进行运算。

delegate int NumberChanger(int n);
namespace DelegateAppl
{
    class TestDelegate
    {
        static int num = 10;
        public static int AddNum(int p)
        {
            num += p;
            return num;
        }
        public static int MultNum(int q)
        {
            num *= q;
            return num;
        }
        public static int getNum()
        {
            return num;
        }
        static void Main(string[] args)
        {
            NumberChanger nc;
            NumberChanger nc1 = new NumberChanger(AddNum);
            NumberChanger nc2 = new NumberChanger(MultNum);
            nc = nc1;
            nc += nc2;
            nc(5);
            Console.WriteLine("Value of Num:{0}", getNum());
            Console.ReadKey();
        }
    }
}
//The result of program is : 75  
//This program is equivalent to adding and then multiplying 

委托的具体用途

using System;
using System.IO;
namespace DelegateAppl
{
    class PrintString
    {
        static FileStream fs;
        static StreamWriter sw;
        //委托声明
        public delegate void printString(string s);
        //读方法打印到控制台
        public static void WriteToSceen(string str)
        {
            Console.WriteLine("The String is :{0}", str);
        }
        //该方法打印到文件
        public static void WriteToFile(string s)
        {
            fs = new FileStream("c:\\message.txt",
                FileMode.Append, FileAccess.Write);
            sw = new StreamWriter(fs);
            sw.WriteLine(s);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        //该方法把委托作为参数,并使用它调用方法
        public static void sendString(printString ps)
        {
            ps("Hello World");
        }
        static void Main(string[] args)
        {
            printString ps1 = new printString(WriteToSceen);
            printString ps2 = new printString(WriteToFile);
            sendString(ps1);
            sendString(ps2);
            Console.ReadKey();
        }
    }
}

以上代码执行结果:Hello World

借用菜鸟教程的一个例子,很好的理解委托多播的原理

public class MrZhang
{
    public static void BuyTicket()
    {
        Console.WriteLine("MrZhang BuyTicket");
    }
    public static void BuyMovieTicket()
    {
        Console.WriteLine("MrZhang BuyMovieTicket");
    }
}
class MrMing
{
    public delegate void BuyTicketEventHandler();   //Declare a delegate,it's a command.
    public static void Main(string[] args)
    {
        BuyTicketEventHandler myDelegate = new BuyTicketEventHandler(MrZhang.BuyTicket);
        myDelegate += MrZhang.BuyMovieTicket;   //At this point the delegate is attached to the specify method
        myDelegate();
        Console.ReadKey();
    }
}
//This program result is:
//MrZhang BuyTicket
//MrZhang BuyMovieTicket

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值