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

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)
3606guest9/20/201511190확장 클래스 네이밍 규칙은 어떻게 하시나요? [1]
3605spow...9/18/201510411익명형식을 다른 클래스의 인스턴스로 전달하고 싶습니다. [1]
3604popo9/11/201511596[C#] Singleton 을 사용하면서 궁금한점이 있어 질문 남깁니다. [2]
3603강준9/11/201510602xamarin 을 시작하려고 하는데 혹시 도움이 될만한 사이트나 정보 있으면 공유해 주세요~~ [1]
3602Mink...9/9/201511736aspx > 다른 확장자로 변경 후 디버깅 때문에 질문 드려 봅니다. [10]
3601edain9/8/201511934캐스팅...에 관해 궁금합니다. [5]
3600guest9/7/201515183C# 버전의 정의는 어떻게 구분하나요? [2]
3599김태훈9/4/201512847안녕하세요. C언어를 배우려고 하는 문과생입니다. [1]
3598미니8/29/2015133466.0은 전체소스? [1]
3597손성수8/28/201511840시작하세요 ! c# 프로그래밍 이책으로 배우고 있는데요 [2]
3596지나가는...8/26/201511340크로스플랫폼 [1]
3594guest8/25/201510982요즘 Windonws Form 기반 N Tier 어떻게 구성하나요?? [1]
3593개발자8/18/201513700현 시점에서 VS 버전은 뭐가 좋을까요? [8]
3592나그네8/15/201513088안녕하세요 질문이 있어서... [4]
3589spow...8/11/201514201Nancy + Razor 사용시 cshtml의 IntelliSense 및 참조 오류 [3]
3590spow...8/12/201513089    답변글 [질문]: (첨부 추가) [1]파일 다운로드1
3588재현8/8/201517939안녕하세요. C# 6.0 책을 보는 중에 의문이 생겨 질문드립니다. [4]
3587주문중8/4/201517977신간 출간 [7]
3586꿈꾸는개...7/29/201512970자바스크립트 checkbox 관련하여 ie10에서 호완이 안되는 부분이 있어 질문드립니다 [1]
3585유동근7/21/201511572TTS오류 [1]파일 다운로드1
3583노영우7/20/201511661iisnode 를 클래식 파이프라인 모드에서 호스팅하기 [2]
3582황희성7/17/201514540동영상 캡쳐에 관련해서 궁금하것이 있습니다. [6]파일 다운로드1
3581popo7/15/201511199[WPF] DependencyProperty에서 callback 처리시 문의 입니다. [1]
3580국왕님7/14/201511430어셈블리 서명 pfx 파일의 가져오기시 암호를 확인할 수 있을까요? [1]
3579고훈용7/13/201510856스마트클라이언트 윈도우8.1 실행오류 해결 방법 문의 [1]파일 다운로드1
3578이성환7/9/201513394WPF Multi Dispatcher 사용 시 hang 발생 [4]파일 다운로드1
... 46  47  48  [49]  50  51  52  53  54  55  56  57  58  59  60  ...