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

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]

... 76  77  78  [79]  80  81  82  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
525독불장군12/15/200614158일주일 공부하고 있는데 잘 안되네요 도와 주세요 스마트 클라이언트파일 다운로드1
528정성태12/19/200614345    답변글 [답변]: 일주일 공부하고 있는데 잘 안되네요 도와 주세요 스마트 클라이언트
530독불장군12/20/200613996        답변글 [답변]: [답변]: 답변에 감사 드립니다.
523주낙현12/12/200613401healthMonitoring 구성섹션을 설정하는 방법 [1]
519이진형12/8/200613084스마트 클라이언트에서 부모 IE 컨트롤 하기
520정성태12/11/200613873    답변글 [답변]: 스마트 클라이언트에서 부모 IE 컨트롤 하기 [3]
517주낙현12/5/200613772Property 재정의가 가능한가요?? [1]
516양덕진11/29/200613391SSL인증서 질문? [1]
515경혜원11/27/200614832화면 캡춰 관련해서 문의 드립니다. [1]
513양덕진11/26/200617035SSL인증문제?
514양덕진11/27/200618855    답변글 [답변]: SSL인증문제? [1]
512윤창일11/24/200616277[ActiveX vs 가장기법] [1]
511방문자11/24/200615100스마트 클라이언트 질문입니다. [3]
509주낙현11/22/200616616스마트 클라이언트에서 WebBrowser 컨트롤사용? [4]
508임장현11/20/200616043서로다른 프로젝트간의 세션공유에 대해 질문드립니다.
510정성태11/22/200614427    답변글 [답변]: 서로다른 프로젝트 간의 세션 공유에 대해 질문드립니다.
504탑.11/10/200615022COM+ 구성 좀 봐주세요...
505정성태11/14/200614787    답변글 [답변]: COM+ 구성 좀 봐주세요...
506탑.11/15/200614120        답변글 [답변]: [답변]: COM+ 구성 좀 봐주세요... [2]
503sagi...11/10/200614004bho 폼 관련 질문입니다/ [1]
501안연준11/7/200614728(SmartClient) 프레임워크 1.1 과 2.0 은 호환이 안된다? [3]
507안연준11/17/200614459    답변글 [답변]: (SmartClient) 프레임워크 1.1 과 2.0 은 호환이 안된다? [1]
498쿠리마10/30/200614810Smart Client를 VC60 MFC Dialog 에서 사용하기
500정성태10/30/200616766    답변글 [답변]: Smart Client를 VC60 MFC Dialog 에서 사용하기 [3]
497이방은10/30/200616925질문 있습니다..ㅡ.ㅠ;
499정성태10/30/200616779    답변글 [답변]: 질문 있습니다..ㅡ.ㅠ; [2]
... 76  77  78  [79]  80  81  82  83  84  85  86  87  88  89  90  ...