// declare MathCalculation instance if not already done private void Awake() { if (instance == null) { instance = this; } }
[TestMethod] // ----------------------------------------------------------------------- public void TestMethod8() // 有沒有Invoke似乎沒差? { MathCalculation add_1 = Calculator.AddNumbers; Func <float, float, double> add_2 = Calculator.AddNumbers; // Func MathCalculation divide = Calculator.DivideNumbers; var result = Calculator.AddNumbers(2, 3); HLog.print("init =" + result); result = add_1.Invoke(2, 3); HLog.print("add_1=" + result); result = add_1(2, 3); HLog.print("add_1=" + result); result = add_2.Invoke(2, 3); HLog.print("add_2=" + result); result = add_2(2, 3); HLog.print("add_2=" + result); result = divide(100, 3); HLog.print("divide=" + result); }
static void Main(string[] args) { //var m = new Math(); //wskazanie dla delegaty do uzywania danej funkcji( metody ) var delPointer = new MathCalculation <int>(Math.Add); //uruchomienie delegaty (zawolanie) delPointer(5, 4); //zarejestruj metode do delegaty delPointer += Math.Multiply; delPointer(6, 6); //lub delPointer.Invoke(6,6); delPointer.Invoke(1, 1); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Lambdy:"); Console.WriteLine(); // LAMBDY //FUNC(delegacje) //podpiecie delegacji od FUNC do metody z klasy Math //to co bedzie jako ostatni arg to bedzie zwracane Func <int, int, int> fnc = Math.AddWithResult; var tResult = fnc(3, 3); Console.WriteLine($"Func<int,int,int> TResult: {tResult}"); Console.WriteLine(); //podpiecie delegata action pod funkcje Action <string> act = System.Console.WriteLine; act("Hello world"); Predicate <string> pdc = String.IsNullOrEmpty; Console.WriteLine(pdc("")); }