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

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)
513양덕진11/26/200617775SSL인증문제?
514양덕진11/27/200619655    답변글 [답변]: SSL인증문제? [1]
512윤창일11/24/200616969[ActiveX vs 가장기법] [1]
511방문자11/24/200615886스마트 클라이언트 질문입니다. [3]
509주낙현11/22/200617489스마트 클라이언트에서 WebBrowser 컨트롤사용? [4]
508임장현11/20/200616879서로다른 프로젝트간의 세션공유에 대해 질문드립니다.
510정성태11/22/200615308    답변글 [답변]: 서로다른 프로젝트 간의 세션 공유에 대해 질문드립니다.
504탑.11/10/200615875COM+ 구성 좀 봐주세요...
505정성태11/14/200615583    답변글 [답변]: COM+ 구성 좀 봐주세요...
506탑.11/15/200614958        답변글 [답변]: [답변]: COM+ 구성 좀 봐주세요... [2]
503sagi...11/10/200614790bho 폼 관련 질문입니다/ [1]
501안연준11/7/200615557(SmartClient) 프레임워크 1.1 과 2.0 은 호환이 안된다? [3]
507안연준11/17/200615276    답변글 [답변]: (SmartClient) 프레임워크 1.1 과 2.0 은 호환이 안된다? [1]
498쿠리마10/30/200615631Smart Client를 VC60 MFC Dialog 에서 사용하기
500정성태10/30/200617737    답변글 [답변]: Smart Client를 VC60 MFC Dialog 에서 사용하기 [3]
497이방은10/30/200617773질문 있습니다..ㅡ.ㅠ;
499정성태10/30/200617571    답변글 [답변]: 질문 있습니다..ㅡ.ㅠ; [2]
495엔틱스10/25/200615139안녕하세요. 세션에 관련해서 질문을 올립니다. [2]
493안연준10/25/200615810스마트클라이언트 배포에서 Config 내용이 이해가 안되요
494안연준10/25/200614015    답변글 [답변]: 스마트클라이언트 배포에서 Config 내용이 이해가 안되요 [2]
489안연준10/23/200615355스마트 클라이언트 배포시 문제점
491안연준10/24/200615944    답변글 [답변]: 스마트 클라이언트 배포시 문제점 [2]
492안연준10/24/200614951        답변글 [답변]: [답변]: 스마트 클라이언트 배포시 문제점
488안연준10/23/200614656닷넷 프레임워크 때문에 일어난 어의없는 상황 [2]
484서민균10/20/200614719스마트 클라이언트 인쇄질문 올린 사람입니다.
486정성태10/22/200616085    답변글 [답변]: 스마트 클라이언트 인쇄질문 올린 사람입니다.
... 76  77  78  [79]  80  81  82  83  84  85  86  87  88  89  90  ...