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

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]

... 91  92  93  [94]  95  96  97 
NoWriterDateCnt.TitleFile(s)
111정성태3/19/20059779    답변글 [답변]: 닷넷 프레임워크 배포
107안연준3/17/20059030[-_-]스마트 클라이언트에 관련 된 질문[-_-]
110정성태3/19/20059831    답변글 [답변]: [-_-]스마트 클라이언트에 관련 된 질문[-_-]
116안연준3/21/20059425        답변글 [답변]: 친절한 답변 고맙습니다.
98김용국3/18/200511175정성태님... 연결이 준비됬습니다
99정성태3/2/200512071    답변글 [답변]: 정성태님... 연결이 준비됬습니다
100김용국3/18/20058907        답변글 [답변]: 죄송합니다! 급히올리느라 file path의 변경을 하지않고 그냥올렸네요....!
101정성태3/2/20059161            답변글 [답변]: [답변]: 죄송합니다! 급히올리느라 file path의 변경을 하지않고 그냥올렸네요....!
102김용국3/18/20059228                답변글 [답변]: 거듭죄송하네요..... 후~~~주소를 변경하고 빌드를 다시 했습니다....
103정성태3/2/20059114                    답변글 [답변]: [답변]: 거듭죄송하네요..... 후~~~주소를 변경하고 빌드를 다시 했습니다....
104김용국3/2/20059325                        답변글 [답변]: 네... 빨리 검토해 봐 주셔서 감사합니다~~
105정성태3/2/20058553                            답변글 [답변]: [답변]: 네... 빨리 검토해 봐 주셔서 감사합니다~~
106김용국3/2/20059024                                답변글 [답변]: 답변감사합니다!
91김용국2/28/200510834IE에 WindowsFormControl을 올려 실행하면 이런에러가 나네요???파일 다운로드1
92정성태2/28/200512139    답변글 [답변]: IE에 WindowsFormControl을 올려 실행하면 이런에러가 나네요???
93김용국2/28/200511079        답변글 [답변]: 답변감사합니다... 재질문을 드립니다
94정성태2/28/200511659            답변글 [답변]: [답변]: 답변감사합니다... 재질문을 드립니다
95김용국2/28/200511735                답변글 [답변]: IE주소창에서 해당주소로 실행을 해보니....디버깅 PopUp화면이...^
96정성태2/28/200510789                    답변글 [답변]: [답변]: IE주소창에서 해당주소로 실행을 해보니....디버깅 PopUp화면이...^
97김용국3/1/200511318                        답변글 [답변]: 준비되는데로 말씀드리겠습니다 ^^
88안지환2/22/200512868^^ 사이트 잘 들러보았습니다.
89정성태2/22/200512833    답변글 [답변]: ^^ 사이트 잘 들러보았습니다.
85한기열2/22/200511445정성태님 홈같은 부드러운 페이지 넘김?은 어떻게 구현하나요?
86정성태2/22/200512163    답변글 [답변]: 정성태님 홈같은 부드러운 페이지 넘김?은 어떻게 구현하나요? [2]
84김용국2/21/200512228Smart Client에 관한 문의 드립니다.
87정성태2/22/200512289    답변글 [답변]: Smart Client에 관한 문의 드립니다.
... 91  92  93  [94]  95  96  97