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

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)
239장강현9/14/200514987SmartClient에서의 배포 문제에 대해 [2]
238이순원9/13/200510351디버깅 할때 native 옵션껐는데두 너무 느려요.. [1]
236코디9/12/200510325[질문]런타임 보안 배포 [1]
237코디9/13/200510873    답변글 [재질문]: [질문]런타임 보안 배포 [1]파일 다운로드1
235임은선9/9/200512925스마트 클라이언트 배포시 환경파일인 app.config는 어떻게 배포 해야하나요...? [2]
234임은선9/7/200511695스마트 클라이언트 보안문제관련해서 질문이 있습니다.. [2]
233이한욱8/31/200519733가상 시리얼 포트(Virtual COM Port) 만들기 [1]
232정준명8/30/200511358[질문] HTTP 모듈/핸들러 또는 RewriteURL 관련입니다. [4]
231이진일8/30/200510482참조한 dll 들은 어떻게 배포하나요...? [1]
2303028/29/200511308[질문] 웹에서 Exchange Server 사용자 추가 [2]
229김재명8/26/200510965성태님의 홈페이지 J & J의경우 보안모듈을 어떻게 해결하셨는지 궁금합니다. [1]
227임규진8/24/200510809리소스 파일에 그림 첨가하는 방법 좀 알려 주세요. [1]
226최인호8/19/200510819스마트 클라이언트 exe 버젼 실행시 시간이 너무 오래 걸립니다. [1]
225코디8/17/200511011[질문]닷넷프레임워크 설치여부 [1]
224박준현8/17/200510610[질문]중첩 그리드라고 해야 할까요? [1]
223매커7/28/200510018강좌 잘 읽었습니다. [1]
222김지영7/27/200511480C#에서 oledb 사용하다가 에러 [2]
221박민우7/26/200512029스마트클라이언트에서의 DB접속 [1]
220테드7/22/200510966스마트 클라이언트에서 원치 않는 패킷 송수신 문제... [3]
219김종욱7/21/200511466안녕하세요... 툴밴드 콤보 박스에 대해.. [1]
2187/21/200510109스마트 클라이언트에 대한 질문 입니다. [1]파일 다운로드1
217왕초보7/21/200510707마소강좌의 AxForSmartClient 를 실행시키고 싶은데요... [1]
215궁금증6/23/200510204도대체 이게 무슨 에러 일까요... [1]
213궁금이6/18/200511900Winform 형태의 NTD에서 사용자 로그인 성공 여부 정보 유지하는 방법 [1]
212궁금이6/16/20059900ActiveX 컨트롤이 NTD에 이상이 없는지요? [1]
211류성태6/15/200510428smart client에 관하여.... [1]
... 76  77  78  79  80  81  82  83  84  85  86  87  88  [89]  90  ...