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

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)
407김용국6/21/200613307                답변글 [답변]: 다시한번 문의드립니다!
398김성호6/17/200613897IE프로그래밍 관련하여 질문이
400정성태6/19/200613668    답변글 [답변]: IE프로그래밍 관련하여 질문이 [5]
396guest6/13/200613466CallByName 을 어떻게 처리해야 할지 ....조언 부탁합니다.
397정성태6/15/200612975    답변글 [답변]: CallByName 을 어떻게 처리해야 할지 ....조언 부탁합니다.
399guest6/19/200612572        답변글 [답변]: [답변]: CallByName 을 어떻게 처리해야 할지 ....조언 부탁합니다.
392이상식6/9/200613409스마트클라이언트 실행환경 관련 질문입니다.
394정성태6/12/200613323    답변글 [답변]: 스마트클라이언트 실행 환경 관련 질문입니다.
395이상식6/12/200613602        답변글 [답변]: [답변]: 스마트클라이언트 실행환경 관련 질문입니다.
391심현철6/9/200613541Win32 platform과 .NET platform간의 성능이 어느정도 차이가 날까요?
393정성태6/12/200613302    답변글 [답변]: Win32 platform과 .NET platform간의 성능이 어느정도 차이가 날까요?
390이현행6/6/200613470정말 답답해서 그렇습니다. [1]
389임경훈6/2/200623365sql과 oracle 사용시 파라메타 차이점
387창민 6/1/200615300안녕하세요.문제가 걸렸는데 해결기미가 안보여서 답답해서.. 혹시 아시면 문제 채질이라도...파일 다운로드1
388정성태6/1/200614897    답변글 [답변]: 안녕하세요.문제가 걸렸는데 해결기미가 안보여서 답답해서.. 혹시 아시면 문제 채질이라도...
385오윤섭5/31/200612551SmartClient 동적호출
386정성태6/1/200613036    답변글 [답변]: SmartClient 동적 호출
380윤용한5/29/200613042ATL Event에서 스크립트가 값을 다시 돌려주는 방법은?파일 다운로드1
381윤용한5/29/200612747    답변글 [답변]: ATL Event에서 스크립트가 값을 다시 돌려주는 방법은?
382윤용한5/29/200613299        답변글 [답변]: [답변]: ATL Event에서 스크립트가 값을 다시 돌려주는 방법은?
383정성태5/30/200613642            답변글 [답변]: [답변]: [답변]: ATL Event에서 스크립트가 값을 다시 돌려주는 방법은?
378guest5/26/200613068managed 와 unmanaged 의 차이가 뭐져?
379정성태5/26/200613335    답변글 [답변]: managed 와 unmanaged 의 차이가 뭐져? [1]
384guest5/30/200612888        답변글 감사합니다.
376노기도5/16/200613081vb6의 GetObjectContext 를 2005에서는 어떻게 사용해야 하나요? [3]
375이덕희5/15/200615288SmartClient 강좌를 보고... 엑박이 나옵니다... [2]파일 다운로드1
... 76  77  78  79  80  81  82  [83]  84  85  86  87  88  89  90  ...