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

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)
1238(non...3/13/201412018(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201412127(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201413007(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201412153(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201414187(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201412302(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201411377(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201412692(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201413059쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201412763(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201411285안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201419918안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201416021(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201411574아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201412425(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201412472(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201411005질문드립니다. [1]
1219이성환2/28/201410683string.Join()과 Enumerable.Aggregate()의 차이가 궁금합니다. [2]파일 다운로드1
1218김형진2/25/201412143안녕하세요. window azure에 대해서 질문이 있어서 문의 드립니다 [4]
1217(non...2/23/201412586(글쓴이의 요청으로 삭제합니다.) [1]
1215아리수2/20/201416125C# 공부하면서 WPF에 대한 질문. [2]
1214조광훈2/20/201414376IIS8 응용프로그램 풀 관련 질문 드립니다. [2]파일 다운로드1
1213김태훈2/17/201411266가상화 프로그램 질문입니다. [1]파일 다운로드1
1212조광훈2/13/201410418ISAPI 필터에서 커스텀 헤더 정보 추가 [1]파일 다운로드1
1211조광훈2/12/201413849isapi 필터 로드 오류 [2]
1208박지호2/9/201417200[오타] 시작하세요 C# 프로그래밍 p.267 ~ 350 [1]
... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...