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

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)
1279(non...6/23/201411674(글쓴이의 요청으로 삭제합니다.) [17]
1278이상식6/19/201412889.net DLL 내 자바스크립트를 수정 또는 재정의 할 수 있을까요? [3]
1277김솔지6/18/201410579silverlight에서 datagrid, listbox질문이여 [2]
1276정우석6/16/201410099쿠키 [1]
1274김솔지6/10/201414580배포 페이지 url을 얻고 싶습니다. [8]
1272이훈모6/7/201410143정말 어려운 상황에 직면했습니다. [1]
1270Jong...6/2/201419606C#과 C++을 이용한 Image 처리. [13]
1269김아영5/29/201410270InitializeComponent 함수 호출 지연 현상 [5]
1268솔솔5/27/20149931smart client [1]
1266김솔지5/22/201411984clickonce 수정에 대해 알고싶습니다. [2]
1265이은아5/22/201415611DataGridView 헤더를 두줄이상으로 하고싶습니다. [1]파일 다운로드1
1264김인호5/18/201413207소스코드 및 예제그림 zip 파일 [1]
1263이영종5/15/201411637159페이지 오타인것 같습니다 [5]
1262(non...5/4/201412488(글쓴이의 요청으로 삭제합니다.) [10]
1261이근주5/4/201410793다시 한번 질문드릴께요. [2]
1259이근주5/1/20149836도서 오류인 것 같네요.. [1]
1258최세정4/28/201412249안녕하세요~php module 오류로 고민하다가 여기까지 왔네요..ㅜㅜ [2]
1252popo4/21/201411514바인딩 질문입니다. [2]
1251(non...4/20/201415637(글쓴이의 요청으로 삭제합니다.) [11]
1249홍용규4/17/201415639app.config 파일 관련 질문 있습니다. [2]
1246(non...3/30/201411961(글쓴이의 요청으로 삭제합니다.) [1]
1245POPO3/26/201411370Http 프로토콜 관련 질문 입니다. [1]
1244(non...3/26/201411475(글쓴이의 요청으로 삭제합니다.) [1]
1241(non...3/22/201415253(글쓴이의 요청으로 삭제합니다.) [4]
1240이석주3/21/201418156인터넷 익스플로러가 hang이 걸리는 현상 문의 [1]파일 다운로드1
1238(non...3/13/201412019(글쓴이의 요청으로 삭제합니다.) [2]
... 46  47  48  49  50  51  52  53  54  [55]  56  57  58  59  60  ...