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

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]

... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1208박지호2/9/201417200[오타] 시작하세요 C# 프로그래밍 p.267 ~ 350 [1]
1207임동찬2/5/201411641Web페이지에서 .net application 실행시키는 방법 [3]
1206신지환2/3/201413533visual sourcesafe(internet) 체크인 에러 [1]
1205박지호2/2/201415059[오타] 시작하세요 C# 프로그래밍 p.199 ~ 202 [1]
1204김태훈1/27/201426529Windows Service 오류 문의입니다. [2]
1203박지호1/26/201416733[오타] 시작하세요 C# 프로그래밍 p.131, 157, 180 [1]
1202이창주1/24/201418240[질문] Windows Error Reporting [8]
1201김나리1/21/201411512[시작하세요 C# 프로그래밍] 비동기 호출 [1]
1200박지호1/19/201416230[오타] 시작하세요 C# 프로그래밍 p.76 [1]
1199윤종현1/9/201412160p654 의 비동기 관련 설명 [3]
1198초이1/4/201412417웹서비스 WSDL 생성및 프록시 클래스 생성 관련 질문입니다. [1]
1196박현수1/2/201411370[WCF] Client 호출 방법 [2]
1195박현수12/23/201311372[WCF] 클라이언트의 호출실패(IIS이용) [4]파일 다운로드1
1191박주만12/18/201318325C++ Dll 에서 C# 의 PictureBox이미지 변경문제 [1]
1193박주만12/19/201312522    답변글 [답변]: C++ Dll 에서 C# 의 PictureBox이미지 변경문제 [2]파일 다운로드1
1190정진호12/10/201310533비동기로 실행할수 있도록 Custom Attribute 를 만들고 싶습니다. [1]
1189Youn...12/10/201311060책을 사기전에 궁금한것이 있습니다. [1]
1188이민석12/5/201312166ocx 를 C#에서 마샬링관련 질문입니다.. [2]파일 다운로드1
1187이성환12/3/201313415WPF WebBrowser control의 자식 창이 close 되기 전 Navgate 재호출 문제 [2]파일 다운로드1
1186박종혁12/2/201311259책의 예제 중에 result 변수가 할당 되었지만 사용되지 않았다고 오류가 납니다!! [1]
1185박은희11/27/201314348멀티바이트로 개발한 프로그램을 유니코드로 변경시 쉽게 처리 하는법 [2]파일 다운로드1
1183박현수11/20/201311570WCF에서 web.config appsetting 읽기 [1]
1184박현수11/20/201311834    답변글 [답변]: WCF에서 web.config appsetting 읽기 [3]파일 다운로드1
1182유창우11/16/201320925자마린이 궁금... [8]
1181허관11/11/201310648책 샀습니다! [1]
1180김형지11/6/201314711안녕하십니까. 프로그램이 실행되지 않아 여쭙고자 합니다ㅠ [1]파일 다운로드1
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...