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

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]

... 16  [17]  18  19  20  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
55126/11/20218020xlwings 가 실행조차 되지 않습니다. ㅠㅠ [7]
5511Syong6/9/20216825User Control에 string array 속성 추가하는 방법 [4]
5510jay6/8/20215846string 문자열에 쌍따옴표(")를 넣고 싶습니다. [1]파일 다운로드1
5509한예지 donator6/3/20215795ExeccuteNonQuery 질문있습니다! [2]
5508최재남6/3/20216634마우스 이벤트 관련 질문 좀 드리겠습니다. [6]
5507한예6/2/20215816static과 스택오버플로우 질문있습니다. [5]파일 다운로드1
5506이창석6/1/20217222센서의 값을 받아서 PC를 통해 모니터링 하는 것을 만들고 있습니다. [1]
5505kss5/31/20215799.net5에서 소멸자가 어떻게 바뀐건가요? [1]
5503xing...5/24/20216174xingapinet 에 수정 요청 부탁 드립니다. [1]
5501한예지 donator5/23/20215932IProgress 사용법이 궁금합니다. [2]
5500한예ㅈ5/23/20217802비동기 코드 흐름 질문있습니다. [3]
5498곰장어5/21/20215662List에 static 변수를 추가했을때의 궁금증 [3]파일 다운로드1
5497지평선5/20/20216907윈도우 배율을 알 수 있을까요? [1]
5496cs린이5/20/20215544C# 8.0 질문입니다. [2]파일 다운로드6
5495Natie5/13/20215073객체를 생성과 동시에 초기화 하는 방법 [1]
5494지예예지5/13/20217344비동기 코드 흐름이 궁금합니다! [2]
5493xing...5/6/20215254xing api XQCSPAT00600 질문입니다 [4]파일 다운로드1
5492한예지 donator5/5/20215338FromCurrentSynchronizationContext 관련 코드 질문있습니다! [2]
5491조우성5/4/20219772WinForm과 WPF의 성능차이 비교한 자료가 있을까요? [4]
5490한예지 donator5/3/20216141UI 스레드의 Invoke 질문있습니다. [4]
5489저누4/28/20216741시작하세요 C# 9.0, 225페이지 구조체 관련 질문드립니다. [2]
5488종범4/27/20216977C# 비동기 함수 async, await 와 Task의 관계에 대해 질문 드립니다. [6]
5487진우4/27/20216339C# 엑셀 자동화 성능 향상 문의 [2]
5486지나가던...4/26/20215894닷넷 구현 코드 관련 질문 [2]
5485이재원4/17/20216271교재 315페이지 내용 질문 [3]
5484Syong4/16/20219820윈폼 기반의 응용프로그램 dll 참조와 32,64bit 빌드 관련 문의 [4]
... 16  [17]  18  19  20  21  22  23  24  25  26  27  28  29  30  ...