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

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)
493안연준10/25/200614007스마트클라이언트 배포에서 Config 내용이 이해가 안되요
494안연준10/25/200612160    답변글 [답변]: 스마트클라이언트 배포에서 Config 내용이 이해가 안되요 [2]
489안연준10/23/200613340스마트 클라이언트 배포시 문제점
491안연준10/24/200614042    답변글 [답변]: 스마트 클라이언트 배포시 문제점 [2]
492안연준10/24/200613047        답변글 [답변]: [답변]: 스마트 클라이언트 배포시 문제점
488안연준10/23/200612778닷넷 프레임워크 때문에 일어난 어의없는 상황 [2]
484서민균10/20/200612921스마트 클라이언트 인쇄질문 올린 사람입니다.
486정성태10/22/200614179    답변글 [답변]: 스마트 클라이언트 인쇄질문 올린 사람입니다.
483guest10/19/200613228asp.net 에서 Com+ 등록된 dll 의 차이점이 무엇인지요?
485정성태10/22/200613861    답변글 [답변]: asp.net 에서 Com+ 등록된 dll 의 차이점이 무엇인지요?
490deve...10/23/200617078        답변글 [답변]: [답변]: asp.net 에서 Com+ 등록된 dll 의 차이점이 무엇인지요? [1]
478서민균10/17/200615064스마트 클라이언트로 만든 컴포넌트가 인쇄가 안되요.....ㅜㅜ [5]
477sagi...10/15/200614286bho 와 mfc 메시지 전송 관련 질문입니다.
479정성태10/17/200616032    답변글 [답변]: bho 와 mfc 메시지 전송 관련 질문입니다.
480sagi...10/17/200613965        답변글 [답변]: 감사합니다. [1]
481sagi...10/19/200613913            답변글 [답변]: 죄송합니다 .. 한가지 더 여쭤 볼께요
482정성태10/19/200613444                답변글 [답변]: [답변]: 죄송합니다 .. 한가지 더 여쭤 볼께요
496sagi...10/27/200614111                    답변글 [답변]: 감사드립니다.
476문태정10/11/200616454FarPointSpread로 출력 시 시트 암호설정문제 [1]
474임경훈10/9/200616974세션값이 유지가 안되는데요? [1]
470쿠리마9/29/200613270고수님들께 질문 올립니다. (C# COM Server에서 C++ Client에게 string맴버 포함한 구조체 배열 넘기기)파일 다운로드1
473정성태10/5/200615306    답변글 [답변]: 고수님들께 질문 올립니다. (C# COM Server에서 C++ Client에게 string맴버 포함한 구조체 배열 넘기기) [3]파일 다운로드1
469이방은9/29/200612943질문이 있어요.. [2]
466이승기9/25/200612721Attribute를 이용한 COM 구현 시 interface의 상속 [1]
467이승기9/27/200612305    답변글 [답변]: Attribute를 이용한 COM 구현 시 interface의 상속
4659/23/200611830vb.net에서 c에서 보내는 Post메쎄지를 잡아서 처리할수 없을가요? [1]
... 76  77  78  79  [80]  81  82  83  84  85  86  87  88  89  90  ...