using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Mathematics
{
delegate int CalcDelegate(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; }
CalcDelegate[] methods;
public Mathematics()
{
methods = new CalcDelegate[] { Mathematics.Add,
Mathematics.Subtract, Mathematics.Multiply, Mathematics.Divide };
}
public void Calculate(char opCode, int operand1, int operand2)
{
switch (opCode)
{
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;
}
}
}
namespace Study1
{
class Program
{
delegate void WorkDelegate(char arg1, int arg2, int arg3);
static void Main(string[] args)
{
Mathematics math = new Mathematics();
WorkDelegate work = math.Calculate;
work('+', 10, 5);
work('-', 10, 5);
work('*', 10, 5);
work('/', 10, 5);
}
}
}
델리게이트를 공부하고 있는데요
왜 이번 예제에서는 Mathematics 클래스의 접근 제한자를 public으로 해서
namespace 밖에서 선언(?)을 한건가요?
그리고 static int Add(int x, int y) { return x + y; }
이 부분에서 static을 지우고 실행하면
methods = new CalcDelegate[] { Mathematics.Add,
Mathematics.Subtract, Mathematics.Multiply, Mathematics.Divide };
이 구문에서 Mathematics.Add 부분이 오류가 발생하던데
이 코드에서 static이 어떤 역할을 해주기에 static을 지우면 오류가 발생하는건가요?
ㅜㅜ static에 대한 개념을 잘 모르겠습니다..
[최초 등록일: ]
[최종 수정일: 10/29/2018]