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

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]

... [91]  92  93  94  95  96  97 
NoWriterDateCnt.TitleFile(s)
185정성태5/9/200510766    답변글 [답변]: 안녕하세요
1815/8/200510343// 꼭쫌 답변해 주세요~~~클라이언트 서버 ... 내용, 이점 들...
183정성태5/9/20059989    답변글 [답변]: // 꼭쫌 답변해 주세요~~~클라이언트 서버 ... 내용, 이점 들...
180최성우5/7/200510506[Q] POST 형식의 데이터 훅킹?
182정성태5/9/200511131    답변글 [답변]: [Q] POST 형식의 데이터 훅킹?
190최성우5/10/200510161        답변글 [답변]: [답변]: [Q] POST 형식의 데이터 훅킹?
177최정희5/4/200510695네트워크 케이블의 연결상태
178정성태5/4/200510843    답변글 [답변]: 네트워크 케이블의 연결상태 [1]
188최정희5/10/200510304        답변글 [답변]: [답변]: 네트워크 케이블의 연결상태
189정성태5/10/200510506            답변글 [답변]: [답변]: [답변]: 네트워크 케이블의 연결상태 [2]
191최정희5/11/200510183                답변글 [답변]: [답변]: [답변]: [답변]: 네트워크 케이블의 연결상태 [1]
175안연준5/3/200510546IE 제어에 대한 궁금 증 ㅡ,.ㅡ;;
179정성태5/4/200511167    답변글 [답변]: IE 제어에 대한 궁금 증 ㅡ,.ㅡ;;
168안연준5/2/200510549[Database] Connection Error파일 다운로드1
169정성태5/2/200510729    답변글 [답변]: [Database] Connection Error
170안연준5/2/200510199        답변글 [답변]: [답변]: 계속 에러가 똑같애요
171정성태5/2/200510919            답변글 [답변]: [답변]: [답변]: 계속 에러가 똑같애요
172안연준5/2/200510730                답변글 [답변]: [답변]: [답변]: [답변]: 계속 에러가 똑같애요파일 다운로드1
173정성태5/2/200510475                    답변글 [답변]: [답변]: [답변]: [답변]: [답변]: 계속 에러가 똑같애요
174안연준5/3/200510475                        답변글 [답변]: [답변]: [답변]: [답변]: [답변]: [답변]: 계속 에러가 똑같애요
165장희석4/22/200511044[질문]ASP에서 ATL 서버 컴퍼넌트로 바이너리 데이타 전달하기
167정성태4/29/200510952    답변글 [답변]: [질문]ASP에서 ATL 서버 컴퍼넌트로 바이너리 데이타 전달하기
160카심4/21/200510462Internet Explorer 에서의 닷넷 Smart Client 개발
163정성태4/22/200510405    답변글 [답변]: Internet Explorer 에서의 닷넷 Smart Client 개발
159신대철4/21/200510206자동 로긴 프로그램
162정성태4/22/200510185    답변글 [답변]: 자동 로긴 프로그램
... [91]  92  93  94  95  96  97