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

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)
3673Sung...12/24/201521657UWP예외 발생 : 'System.Runtime.InteropServices.COMException'(mscorlib.ni.dll) 해결방법 [2]
3672Sung...12/23/201519880global::System.Diagnostics.Debugger.Break();가 노랗게 표시될시 [2]
3671이상준12/22/201525021한글 키보드 입력에 대해서 질문이 있습니다. [3]
3670Sung...12/18/201520193UWP 주기적으로 신호를 보내고 클라이언트에서 신호가 오면 받는 프로그램을 하려고 합니다. [5]
3669다비드12/18/201519312인터넷 임시파일 삭제 관련 질문. [1]
3668김치사발면12/16/201518706템플릿이 자꾸 초기화? 되버리네요 [1]
3667김치사발면12/15/201518336템플릿 설정 추가 질문 [1]파일 다운로드1
3666노태현12/15/201517617사용자별로 권한을 주고 볼 수 있는 데이터를 제한하려면 어떤 방법으로 접근하는게 좋을까요? [2]
3665김치사발면12/14/201520941C# 템플릿 설정이 이상해요ㅜㅜ [3]파일 다운로드1
3664KuLu12/10/201517597멀정하던 사이트 중 일부메뉴가 응답없음이 되어버립니다. [3]
3663Sung...12/10/201519647UWP 서버 프로그래밍 작업 중 [5]
3662Sung...12/9/201519210uwp앱에서 접속한 클라이언트 주소값을 받아서 표시하고 싶은데 어떤 메소드를 써야 할까요? [7]
3661김무진12/9/201519120Oracle 환경에서 데이터를 조회할때 한글이 ? 이렇게 표시가됩니다. [1]
3660질문자12/7/201521271OpenFileDialog 호출시 hang걸리는 문제 [4]
3659Sung...12/4/201521057UWP 앱에서 textBox로 클라에서 받은 값을 나타내고 싶은데 안되고 있습니다. [5]
3658강준12/3/201519705Visual Studio (Xamarin) vs Eclipse [2]
3656DEVY...12/1/201519494MasterPageFile 사용시 다국어 처리 질문입니다. [1]
3653DEVY...11/26/201521473ds:Signature 질문입니다. [7]
3654윤용한11/27/201521441    답변글 [답변]: ds:Signature 질문입니다. [3]
3657윤용한12/1/201522941    답변글 [답변]: ds:Signature 질문입니다. [1]
3651노태현11/20/201554026MariaDB - ASP.NET오류의 원인조차 못 찾고 있습니다.. [2]파일 다운로드2
3652노태현11/20/201521231    답변글 [답변]: MariaDB - ASP.NET오류의 원인조차 못 찾고 있습니다.. [3]
3649kokon11/17/201520661예제 파일 실행이 안 되네요 [5]
3647Sang...11/15/201518541Part 3 목차? [5]
3646힘찬도약11/13/201524270c# mscorlib System.IO IOException [8]파일 다운로드2
3644힘찬도약11/11/201523008c# user.config파일 [2]
... 46  47  [48]  49  50  51  52  53  54  55  56  57  58  59  60  ...