亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果將兩個類的函數放在一個委托中會發生什么?

如果將兩個類的函數放在一個委托中會發生什么?

C#
HUH函數 2022-01-09 14:48:00
我正在準備考試,我必須檢查各種代碼。一個是關于 C# 中的委托 - 我看不到它的作用,因為我不知道您是否可以將來自兩個不同類的函數放在一個委托中。這是代碼:namespace konzolnaApplikacijaDelegateVoidMain {public delegate int MyDelegate(int x);class Program{    public int number;    public Program (int x)    {        number = x;    }    public int Add(int x)    {        return x + 10;    }    public int Substract(int x)    {        return x - 10;    }    public int Multiply(int x)    {        return x * 2;    }    static void Main(string[] args)    {        MyDelegate delegate;        Program first = new Program(20);        Program second = new Program(50);        delegate = first.Add;        delegate += second.Add;        delegate -= first.Substract;        delegate += second.Multiply;        delegate += first.Add;        delegate(first.number);        delegate(second.number);        Console.Write("{0}", first.number + second.number);    }  }}
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

代表很簡單。考慮以下委托的實現。


namespace DelegateExamples

{

    class Program

    {

        //Declare a integer delegate to handle the functions in class A and B

        public delegate int MathOps(int a, int b);

        static void Main(string[] args)

        {

            MathOps multiply = ClassA.Multiply;

            MathOps add = ClassB.Add;

            int resultA = multiply(30, 30);

            int resultB = add(1000, 500);

            Console.WriteLine("Results: " + resultA + " " + resultB);

            Console.ReadKey();

        }

    }

    public class ClassA

    {

        public static int Multiply(int a, int b)

        {

            return a * b;

        }

    }

    public class ClassB

    {

        public static int Add(int a, int b)

        {

            return a + b;

        }

    }

}


查看完整回答
反對 回復 2022-01-09
  • 1 回答
  • 0 關注
  • 196 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號