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

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)
456guest9/7/200616243데이타셋 암호화 방법...이 있을까요? [2]
451임은주8/31/200615841mms 스트림을 로컬 파일로 저장해주는 프로그램 에 대해서 여쭤볼께요 [1]
448guest8/28/200616448서버인증서및클라이언트인증서 발급
449정성태8/28/200615825    답변글 [답변]: 서버인증서및클라이언트인증서 발급
450ligh...8/29/200616198        답변글 [답변]: [답변]: 서버인증서및클라이언트인증서 발급 [3]
468light9/28/200616133            답변글 [답변]: [답변]: [답변]: 서버인증서및클라이언트인증서 발급
472정성태10/5/200616665    답변글 [답변]: 서버 인증서 및 클라이언트 인증서 발급
475ligh...10/11/200615568        답변글 [답변]: [답변]: 서버인증서및클라이언트인증서 발급
446윤경재8/20/200621063COM+ 프로젝트 디버깅 방법. [2]파일 다운로드1
442이남호8/7/200615933스마트클라이언트에 Farpoint Winform을 이용했는데 배포가 안되요.
443이남호8/7/200617877    답변글 [답변]: 스마트클라이언트에 Farpoint Winform을 이용했는데 배포가 안되요.
441강혜영8/5/200616879SHDocVw를 이용한 익스플로어 제어
444정성태8/7/200614913    답변글 [답변]: SHDocVw를 이용한 익스플로어 제어
445강혜영8/8/200615610        답변글 [답변]: [질문]: SHDocVw를 이용한 익스플로어 제어 [1]
437혀기7/25/200614208DirectoryEntry의 Childrend에 Add할때 엑세스가 거부됩니다~ㅜㅜ [2]파일 다운로드1
436조성택7/24/200614037IE를 가로채서 그리고 싶을때.. [1]
439태기7/25/200614745    답변글 [답변]: IE를 가로채서 그리고 싶을때..(재질문) [1]파일 다운로드1
435이영균7/21/200615415작그마한 스마트클라이언트 프로젝트를 진행하고 있습니다. [1]파일 다운로드1
431혁이7/19/200614099UpdatePanel(Atlas)위의 SmartClient가 이벤트후 사라집니다. ㅜㅜ파일 다운로드1
432정성태7/19/200614485    답변글 [답변]: UpdatePanel(Atlas)위의 SmartClient가 이벤트후 사라집니다. ㅜㅜ
433혁이7/20/200615246        답변글 [답변]: [답변]: UpdatePanel(Atlas)위의 SmartClient가 이벤트후 사라집니다. ㅜㅜ
434정성태7/20/200616261            답변글 [답변]: [답변]: [답변]: UpdatePanel(Atlas) 위의 SmartClient가 이벤트 후 사라집니다. ㅜㅜ [1]
429kwt7/12/200615480ActiveX 개발을 배우고 싶은데요 [3]
427이홍자7/11/200615680스마트 클라이언트 실행 시 그리드의 체크박스가 안보여요.. [1]
426정보문7/11/200616421액티브X 설치2 [1]
423이방은7/9/200615338리소스 임베디드 질문요.. [1]파일 다운로드1
... 76  77  78  79  80  [81]  82  83  84  85  86  87  88  89  90  ...