using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cs2
{
public class MatheMatics
{
delegate int Mathdelegate(int x, int y);
static int Add(int x, int y) { return x + y; }
static int Subtract(int x, int y) { return x - y; }
static int Multiply(int x, int y) { return x * y; }
static int Divide(int x, int y) { return x / y; }
Mathdelegate[] methods;
public MatheMatics()
{
methods = new Mathdelegate[] { MatheMatics.Add, MatheMatics.Subtract,
MatheMatics.Multiply, MatheMatics.Divide };
}
public void Calculator(char opt, int operand1, int operand2)
{
switch (opt)
{
case '+':
Console.WriteLine("+: " + methods[0](operand1, operand2));
break;
case '-':
Console.WriteLine("-: " + methods[1](operand1, operand2));
break;
case '*':
Console.WriteLine("*: " + methods[2](operand1, operand2));
break;
case '/':
Console.WriteLine("/: " + methods[3](operand1, operand2));
break;
default:
Console.WriteLine("잘못된 입력입니다.");
break;
}
}
}
class Program
{
delegate void newdelegate(char opt, int operand1, int operand2);
static void Main(string[] args)
{
MatheMatics math = new MatheMatics();
newdelegate one = math.Calculator;
one('+', 5, 10);
one('-', 10, 5);
one('*', 1, 5);
one('/', 5, 1);
}
}
}
delegate 부분 공부하고 있는데 이 코드에서
static int Add(int x, int y) { return x + y; }
static int Subtract(int x, int y) { return x - y; }
static int Multiply(int x, int y) { return x * y; }
static int Divide(int x, int y) { return x / y; }
Mathdelegate[] methods;
public MatheMatics()
{
methods = new Mathdelegate[] { MatheMatics.Add, MatheMatics.Subtract,
MatheMatics.Multiply, MatheMatics.Divide };
}
생성자에서 메서드 접근 코드가 MatheMatics.Add -> class.메서드 라는건 static 빼박 의미하는것 this.Add라고 하고 static을 빼면 될려나?
static 을 뺏으면...methods = new Mathdelegate[] {Add, Subtract,Multiply, Divide }; 이런식으로 되어야지..그냥 static만 뺀거아님?
this.add 나 add 나 동일
감사합니다~