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

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)
756손승휘3/24/200818735Microsoft Web Browser OCX 부분이 안전하지 않는 ACTIVEX로 IE에서 인식되어는 점 [1]
754단테3/19/200815876실행파일의 아이콘을 프로그램 적으로 변경할 수 있을까요? [1]
753신동열3/12/200816606비스타의 권한 상승 관련 질문 있습니다. [2]
751김재영3/6/200815728스레드와 비스레드간의 대기상태 순서에 대해 질문이 있습니다. [2]파일 다운로드1
752정성태3/7/200815315    답변글 [답변]: 스레드와 비스레드 간의 대기 상태 순서에 대해 질문이 있습니다. [1]
750김용현3/3/200814664스마트 클라이언트로 파일 다운로드시 파일의 크기가 0인 경우가 발생합니다. [2]
749임영한2/28/2008302132003서버에서 msxml3.dll 오류 "80070005" 문제입니다. [2]
748류재상2/27/200819278Remoting , Tcp Channel 사용시.. 외부에서 접근 보안문제... [3]
747한귀순2/20/200817648WinForm - DataGridView 소계 등 [3]
746박상영2/20/200815577리모팅 관련해서 [3]파일 다운로드1
745조승현2/20/200816268Response.Charset ANSI로 설정..하는부분이 어려워서요.. [1]
744오야2/5/200814892멤버십, 웹파트등의 생성테이블과 업무테이블간의 관계를 어떻게.. [2]
743김기룡1/29/200816722동일 이미지 판단 방법 관련 [2]파일 다운로드1
740경혜원1/28/200818135C#에서 이런게 가능 할런지요?
742정성태1/29/200816183    답변글 [답변]: C#에서 이런게 가능 할런지요? [1]
738임경훈1/25/200817026웹서비스에서 오라클DB에 연결할 때
741임경훈1/28/200816569    답변글 [답변]: 웹서비스에서 오라클DB에 연결할 때 [1]
737단테1/25/200817980안녕하세요 정성태님. C Run-time Library의 배포에 대해서 질문있습니다. [3]
736김현기1/24/200815700XML 접두어 포함한 XmlElement 작성법? [1]
735정명원1/9/200815520ActiveX 사용시 ... [2]
734제영한12/17/200715519웹서비스에서 사용자 정의 예외 사용하기 [2]
732김기룡12/3/200718569WebRequest 요청시 일본어 인코딩 관련 문의파일 다운로드1
733김기룡12/3/200714661    답변글 일어가 깨졌네요..테스트 문자 추가합니다.^^;;파일 다운로드1
739adnim1/27/200816117    답변글 [답변]: WebRequest 요청시 일본어 인코딩 관련 문의 [1]
731우기11/27/200713327[질문] clickonce 배포시 .net framework 2.0, 3.0 설치 시간 줄일 수 있는 묘수없을까요?? [1]
730김재영11/10/200714150스레드에 사용되는 함수에 값을 같이 넘기는 방법이 있습니까?? [2]파일 다운로드1
... 61  62  63  64  65  66  67  68  69  70  71  [72]  73  74  75  ...