Microsoft MVP성태의 닷넷 이야기
C# 7.1 185~187p 질문입니다. [링크 복사], [링크+제목 복사],
조회: 16051
글쓴 사람
꾸엉
홈페이지
첨부 파일
 

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]


비밀번호

댓글 작성자
 



2018-10-29 10시22분
namespace는 임의 관리 단위일 뿐 프로그램의 논리와는 상관이 없는 부분입니다. 그리고 static을 빼면 instance 멤버가 되기 때문에 그 앞에 클래스 이름을 붙일 수 없습니다. 따라서 "Mathematics.Add"라고 하면 안 되고 그냥 "Add"라고 해야 합니다.


정성태
2018-10-29 01시00분
[꾸엉] 감사합니다~!
[guest]

... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1246(non...3/30/201419542(글쓴이의 요청으로 삭제합니다.) [1]
1245POPO3/26/201418899Http 프로토콜 관련 질문 입니다. [1]
1244(non...3/26/201418616(글쓴이의 요청으로 삭제합니다.) [1]
1241(non...3/22/201422042(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201424914인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201418987(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201418960(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201419827(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201418477(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201422652(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201420372(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201419181(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201420170(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201420606쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201421669(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201419448안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201429585안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201425141(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201419456아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201419662(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201420252(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201418683질문드립니다. [1]
1219이성환2/28/201418003string.Join()과 Enumerable.Aggregate()의 차이가 궁금합니다. [2]파일 다운로드1
1218김형진2/25/201419965안녕하세요. window azure에 대해서 질문이 있어서 문의 드립니다 [4]
1217(non...2/23/201420463(글쓴이의 요청으로 삭제합니다.) [1]
1215아리수2/20/201423965C# 공부하면서 WPF에 대한 질문. [2]
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...