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

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)
988WooY...8/17/201118625성태님~ 작은 부탁 하나 있습니다~ [2]
987에스패스트8/3/201120779비밀글쓰기가 있으면 참좋을탠대 아쉽네요 ^^ 저 한가지만더.. [5]
986에스패스트8/3/201124112안녕하세요 ^^ 이렇곧도 있군요 ~ 참좋네요 질문 한가지 여쭤볼게요 [6]
985이성환7/27/201120176P/Inovke 관련 질문입니다. [4]파일 다운로드1
983이성환7/13/201130028Mouse이벤트 관련해서 질문있습니다. [10]파일 다운로드2
982궁굼이7/12/201120411TFS2010 사용관련 [2]
981김창욱7/11/201123707주식형태의 프로그램 처럼 SQL서버의 특정 필드 데이터의 변화가 있을때 재 클라이언트가 정보를 갱신 할 수 있게 하는 방법은 없을까요? [2]
980YJ7/7/201123168App Pool idle time 과 WCF 서비스의 비동기 function 호출. [3]
979왕초보7/1/201121482Apache + IIS Redirect [2]
9776/16/201124860웹컨트롤 어느거 쓰는게 좋나요 [1]
976박성준6/13/201120358VS2008 Add-in 구현 관련 질문 [4]
972김길6/6/201122778메모리 해제 예외 처리 관련.. [2]
971강동원5/29/201120207firebird install건 [1]
970임동찬5/18/201121097ASP.net 솔루션 디버깅 관련 [1]
969이성환5/4/201122619WMI 를 사용하지 않고 하드웨어 정보를 가져올 수 없을까요? [3]
968김동미4/28/201122007안녕하세요 다시 한번 문의를 드립니다.. [2]파일 다운로드1
967임동찬4/22/201124738C# using문 관련 [9]
964김동미4/18/201122484wcf IsOneWay 속성관련 문의 입니다..
965정성태4/18/201123984    답변글 [답변]: wcf IsOneWay 속성관련 문의 입니다..
966김동미4/19/201120979        답변글 [답변]: [답변]: wcf IsOneWay 속성관련 문의 입니다.. [1]
963최재훈4/12/201120059wcf inactivityTimeout 설정시 문의 사항이 있습니다. [2]
962임동찬4/8/201119772TFS 사용관련 [1]
961임동찬4/7/201119903XSD & XML & XmlCodeGenerator [2]
960임동찬4/5/201121901XML Schema Editor [4]
959immm3/24/201119505로그인 연동 어려운 건가요? [1]
958꼭지3/3/201121753Supporting compressed request in WCF 3.5 [5]
... 61  62  63  64  [65]  66  67  68  69  70  71  72  73  74  75  ...