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

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]

... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
852임동찬1/6/201015418base.OnStart(agrs) [1]
851한귀순1/6/201020888oracle & transaction 질문 [5]
850박한주12/31/200918021COM과 C#간 권한 문제에 관해 여쭤봅니다. [5]
848날쌘돌이12/22/200918934C#으로 개발하는 ActiveX 디버깅 문제 [5]
847최승문12/16/200917820winform에서 웹서비스를 이용할때, 객체가 전송안되는 문제... [2]
846이강구12/14/200916762ATL COM 제작 문의 [3]
845김재영12/9/200919593ClickOnce에서 WinForm을 배포시 변동되는 Args값을 넘길 수 있나요? [3]파일 다운로드1
843김재영12/2/200917117Hyper-V 사용에 대해여 질문이 있습니다. [2]
842한귀순12/1/200919953Attempted to read or write protected memory [2]
841장근배11/30/200921541첨부 파일 Download시 한글 Name [2]
844장근배12/3/200916108    답변글 [답변]: 첨부 파일 Download시 한글 Name파일 다운로드1
840박영민11/20/200915883웹 애플리케이션 질문입니다. [1]
837left...10/21/200916796아래 게시글 관련하여 추가 질문 [3]
836left...10/19/200917805ActiveX 상에서 암시적 dll 로드 [1]
832김우진10/15/200914490C# 에서 swa + security 구현 방법이 없을까요?
833정성태10/15/200918574    답변글 [답변]: C#에서 swa + security 구현 방법이 없을까요? [1]
834김우진10/15/200914601        답변글 예제 메시지 올려드릴께요. [2]파일 다운로드1
838이영우10/30/200914508            답변글 국세청 전자세금계선서에 관하여 문의드립니다.
831채승수10/13/200916795스마트 클라이언트 GAC 등록 문제 여쭤봅니다... [2]
829박찬용10/9/200929283WebBrowser 컨트롤 사용시 새창을 띄울시 세션/쿠키 공유가 왜 안될까요?? [1]파일 다운로드1
827송창훈9/24/200914324[질문]CAS 설정
828정성태9/28/200917666    답변글 [답변]: [질문]CAS 설정 [1]
825김재영9/18/200915214프레임워크별 비트 지원에 대해서 문의를 드립니다. [2]
821채승수8/27/200914692클릭원스를 비스타에서 실행시 [1]
822채승수8/28/200919535    답변글 [답변]: 클릭원스를 비스타에서 실행시 [2]
818개발돌이8/14/200917235ActiveX개발한 dll을 임베디드 할때 UI에 대한 질문 [1]파일 다운로드1
... 61  62  63  64  65  66  67  68  [69]  70  71  72  73  74  75  ...