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

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)
231이진일8/30/200511269참조한 dll 들은 어떻게 배포하나요...? [1]
2303028/29/200512366[질문] 웹에서 Exchange Server 사용자 추가 [2]
229김재명8/26/200511830성태님의 홈페이지 J & J의경우 보안모듈을 어떻게 해결하셨는지 궁금합니다. [1]
227임규진8/24/200511738리소스 파일에 그림 첨가하는 방법 좀 알려 주세요. [1]
226최인호8/19/200511658스마트 클라이언트 exe 버젼 실행시 시간이 너무 오래 걸립니다. [1]
225코디8/17/200511995[질문]닷넷프레임워크 설치여부 [1]
224박준현8/17/200511493[질문]중첩 그리드라고 해야 할까요? [1]
223매커7/28/200510914강좌 잘 읽었습니다. [1]
222김지영7/27/200512356C#에서 oledb 사용하다가 에러 [2]
221박민우7/26/200512918스마트클라이언트에서의 DB접속 [1]
220테드7/22/200512015스마트 클라이언트에서 원치 않는 패킷 송수신 문제... [3]
219김종욱7/21/200512367안녕하세요... 툴밴드 콤보 박스에 대해.. [1]
2187/21/200511011스마트 클라이언트에 대한 질문 입니다. [1]파일 다운로드1
217왕초보7/21/200511619마소강좌의 AxForSmartClient 를 실행시키고 싶은데요... [1]
215궁금증6/23/200511210도대체 이게 무슨 에러 일까요... [1]
213궁금이6/18/200512914Winform 형태의 NTD에서 사용자 로그인 성공 여부 정보 유지하는 방법 [1]
212궁금이6/16/200510835ActiveX 컨트롤이 NTD에 이상이 없는지요? [1]
211류성태6/15/200511426smart client에 관하여.... [1]
206헤커의작...6/14/200510335[질문] 안녕하세요. 성태님의 홈페이지에서 화면전환시 처리되는 것에 관련된 건 [3]
205김종욱 6/12/200510838com+ 에서 서버 소켓을 열고 싶고요... com+에서 thread는 어떻게 돌리는지 알고 싶습니다
209정성태6/14/200510850    답변글 [답변]: com+에서 서버 소켓을 열고 싶고요... com+에서 thread는 어떻게 돌리는지 알고 싶습니다
204정현일6/9/200511110Smart Client 실행시 SecurityException파일 다운로드1
208정성태6/14/200511438    답변글 [답변]: Smart Client 실행시 SecurityException [1]
203나그네6/9/200511342태오 사이트 Smart Client 강좌중에서요
207정성태6/14/200510881    답변글 [답변]: 태오 사이트 Smart Client 강좌중에서요
210나그네6/15/200511084        답변글 [답변]: [답변]: 태오 사이트 Smart Client 강좌중에서요파일 다운로드1
... 76  77  78  79  80  81  82  83  84  85  86  87  88  [89]  90  ...