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

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)
213궁금이6/18/200512476Winform 형태의 NTD에서 사용자 로그인 성공 여부 정보 유지하는 방법 [1]
212궁금이6/16/200510470ActiveX 컨트롤이 NTD에 이상이 없는지요? [1]
211류성태6/15/200511073smart client에 관하여.... [1]
206헤커의작...6/14/200510041[질문] 안녕하세요. 성태님의 홈페이지에서 화면전환시 처리되는 것에 관련된 건 [3]
205김종욱 6/12/200510573com+ 에서 서버 소켓을 열고 싶고요... com+에서 thread는 어떻게 돌리는지 알고 싶습니다
209정성태6/14/200510566    답변글 [답변]: com+에서 서버 소켓을 열고 싶고요... com+에서 thread는 어떻게 돌리는지 알고 싶습니다
204정현일6/9/200510778Smart Client 실행시 SecurityException파일 다운로드1
208정성태6/14/200511168    답변글 [답변]: Smart Client 실행시 SecurityException [1]
203나그네6/9/200510996태오 사이트 Smart Client 강좌중에서요
207정성태6/14/200510601    답변글 [답변]: 태오 사이트 Smart Client 강좌중에서요
210나그네6/15/200510803        답변글 [답변]: [답변]: 태오 사이트 Smart Client 강좌중에서요파일 다운로드1
198이지훈6/2/2005100422003이랑 2005랑 함께 사용하는 방법이 없을까요?
201정성태6/6/200510647    답변글 [답변]: 2003이랑 2005랑 함께 사용하는 방법이 없을까요?
197이지훈6/2/2005107092005 에서 컴포넌트 제작
200정성태6/6/200510192    답변글 [답변]: 2005 에서 컴포넌트 제작
196김종욱5/24/200511361익스플러 툴 벤드 제작... for .NET [1]파일 다운로드1
199정성태6/6/200510840    답변글 [답변]: 익스플러 툴 벤드 제작... for .NET
202김종욱6/9/200511354        답변글 [답변]: [답변]: 익스플러 툴 벤드 제작... for .NET [1]
194홍지철5/18/200510563IE에서 Winform control embedded작성시 문제점
195정성태5/19/200511147    답변글 [답변]: IE에서 Winform control embedded작성시 문제점
192헤헤5/13/200510742이것좀봐주세요
193정성태5/13/20059853    답변글 [답변]: 이것좀봐주세요
186기범5/9/200510456안녕하세요2
187정성태5/9/200510931    답변글 [답변]: 안녕하세요2
184기범5/9/200510391안녕하세요
185정성태5/9/200510766    답변글 [답변]: 안녕하세요
... 76  77  78  79  80  81  82  83  84  85  86  87  88  89  [90]  ...