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

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]

... 61  62  63  64  65  [66]  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
956윤용한2/18/201124106WaitHandle.WaitOne 과 Stopwatch에 관한 질문 [3]
955최광욱2/17/201120771TFS 에서 소스 영구 제거 방법 [1]
954한장우2/16/201118438atl activeX 질문이요~ [1]
952박용운2/16/201119341IE8.0에서 BHO [1]
953박용운2/16/201119233    답변글 [답변]: IE8.0에서 BHO
951임동찬2/11/201119501WCF Service Reference [1]
950이성환2/9/201121013Windows application 프로젝트를 참조 했을 때 생성되는 실행파일을 직접 실행 불가능하도록 하고 싶습니다. [6]파일 다운로드1
947김순조1/24/201119820.NET based Com에서 Native ActiveX로 이벤트 보내기?? [2]파일 다운로드1
943김기룡1/3/201123173닷넷 에러시 조치사항관련... [2]
942김기룡12/27/201018200Thread 안정성 관련 문의 드립니다. [2]
941최광욱12/20/201017933정성태님 올리신 글중에 [1]
940최광욱12/20/201019864Assembly Unloading 관련해서 [2]
939최광욱12/20/201018072IIS 로그 읽기 [1]
938날쌘돌이12/14/201019440자바로 asp.net 인증하기 [3]
935김기룡12/13/201029816c#에서 c++로 개발된 dll에 byte[] 전달 관련하여 문의 드립니다. [6]
934임동찬12/7/201017270System.Reflection.Assembly.GetTypes() 메서드에 대해 [1]
929김준호12/2/201017211안녕하세요 비주얼베이직 2005 닷넷 관련 문의입니다 [1]
928김준호11/30/201017786안녕하세요 비주얼베이직 2005 닷넷 관련 문의입니다 [1]파일 다운로드1
927임동찬11/30/201019264Windows\\Temp 폴더의 이름모를 .tmp 파일들에 대해서 [1]
926이승규11/26/201039253IP접속 시도시 ORA-12504 에러 [1]
925임동찬11/11/201016727다른 프로그램의 컨트롤 건드려보기_추가질문(2) [1]
924임동찬11/10/201017490다른 프로그램의 컨트롤 건드려보기_추가질문 [1]
923임동찬11/9/201020098다른 프로그램의 컨트롤 건드려보기 [1]
922박태근11/2/201018670html5의 shape파일 관련 [1]파일 다운로드1
921박태근11/1/201019273DataTable 의 Binary변환! [1]
920김재영10/26/201019713GAC에 등록된 어셈블리를 Visual Studio에서 참조 대화상자에 보이게 할려면 어떤 방법이 있습니까? [2]
... 61  62  63  64  65  [66]  67  68  69  70  71  72  73  74  75  ...