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

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]

... 31  32  33  34  35  36  37  38  39  40  [41]  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
4829윤현수4/26/201714673System.Text.Encoding 질문 [1]
4828jaem...4/20/201710916시스템 종료시 block 함수 xp 지원가능한 방법 질문 [2]
4827ds4/13/201712696매서드 정의로 이동시 [메타데이터에서]에서 라고 나오는 것은 뭔가요? [1]
4826김솔지4/11/201710057CxImage 문자 출력 [1]
4825플메4/10/201710446덱스트 업로드 하는데 구성요소 서비스에 엑셀이 안뜨네요..ㅜㅜ [1]파일 다운로드1
4824김상경4/9/20179833win7에서 vb6.0을 이용해서 프로그램을 만들었습니다.(제가 만든건 아니고 옆 동료가 만들었습니다.) [1]파일 다운로드1
4823조상현3/28/201710875windows event log 에 대한 정보는 어디서 얻을 수 있을까요? [1]
4822부우3/24/201712526글수정이 안되어 다시올려드립니다ㅠ [12]파일 다운로드2
4821부우3/22/201726158파일이나 어셈블리 중 하나를 로드에러 좀 봐주세요 [9]파일 다운로드1
4820듀이3/20/201710947오차 없이 동시에 명령을 보내게 가능한가요? [2]파일 다운로드1
4819popo3/20/201712289C# sslstream 사용시 Cipher List 설정 [3]
4818부우3/16/201721155DllImport 사용시 진입점을 찾을수 없습니다 [2]
4817yun3/15/201710510서피스프로4의 HYPER-V로 윈도우7을 깔았는데... [1]
4816부우3/15/201713239C++ DLL함수 사용 질문드립니다 [10]
4815김지용3/14/20179302.net core linux 오류 [2]
4814김솔지3/13/201710382인쇄 시 , 출력화면을 이미지 파일로 저장하고 싶습니다. [1]파일 다운로드1
4813spow...3/8/201711925멀티 스레드 환경에서 처리하여야 할 목록을 효율적으로 사용하는 기법 질문 드립니다 [2]
4812popo2/23/201711980.NET Window ClassName 변경 질문 드립니다. [1]
4809힘찬도약2/15/201715048C# 가상 시리얼 [4]
4808이길용2/9/201713788WCF 외부 접속 관련해서 질문드립니다ㅠ [1]
4807헬조선식...2/8/201720380WPF의 속도에 대해서 궁금합니다. [3]
4803헬조선식...2/7/201713796WPF로 메신져 메세지 박스 구현 문의 [2]
4802spow...2/6/201710829특정 범위의 값을 다른 값으로 치환하는 메소드를 만들어야 할 때 가장 빠른 방법은? [7]
4804spow...2/7/201710704    답변글 [답변]: 특정 범위의 값을 다른 값으로 치환하는 메소드를 만들어야 할 때 가장 빠른 방법은?
4805spow...2/7/201710377    답변글 [답변]: 특정 범위의 값을 다른 값으로 치환하는 메소드를 만들어야 할 때 가장 빠른 방법은? (글삭제가 안돼 재첨부합니다)파일 다운로드1
4801spow...2/3/201710256C#의 참조 동작관련하여 아이디어 문의 드립니다 [4]
... 31  32  33  34  35  36  37  38  39  40  [41]  42  43  44  45  ...