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

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]

... 31  32  33  34  35  36  37  38  39  [40]  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
4854heyh...7/6/20179504
4853heyh...7/5/20179622성태님이 작성한대로 해봤습니다. [1]파일 다운로드1
4852김레오7/4/201711793서드파티 dll 디버깅에 대해 질문드립니다. [2]
4851김현준7/3/201710896Datagridview VirtualMode 시 GC가 계속 호출되는 현상이 이해가 안갑니다. [2]
4850heyh...7/3/201710641성태님 밑에 질문드렸던 오류입니다. [1]파일 다운로드1
4849포플러7/2/201729327C#으로 만든 프로그램이 어느 순간 속도가 느려지거나 멈춤현상이 있습니다. [4]
4848윤진영7/2/201712275Microsoft Visual C++ 6.0 무설치 관련 [1]
4847heyh...6/30/201710285아무리 고민해도 답이 안나와서.. 질문 드립니다 [1]
4846heyh...6/28/201710317Clickonce에서 Clickonce로 변수 전달 [1]
4845heyh...6/26/20179997vb.net 에서 manifest 제거 방법 [1]파일 다운로드1
4844san6/21/20179422part3 pdf파일로 보는데 눈아퍼요.... 활자로 보고싶어요 [2]
4843윤현수6/20/20179651socket통신에 관한 질문입니다. [1]
4842오세운6/7/201710346로그인폼 다시 질문드려요. [1]
4841popo6/7/201710103궁금한사항이 있어 질문 드립니다. [1]
4840윤현수6/5/201710112datagridview에 바인딩시킨 dataset.table에 관련된 질문입니다. [2]파일 다운로드1
4839오세운6/2/201711022로그인폼 만들기 [1]
4838후배5/29/201711747PDF Reader 라이브러리.. [3]
4837KDP5/29/201711251c++ to C# 컨버팅 문의 [2]
4836popo5/26/201714381리플렉션 성능 관련 질문 드립니다. [2]
4835guest5/25/201710106wpf Canvas 질문드립니다. [1]
4834부탁드립...5/19/201711021익스체인지 서버 구동중 인증서를 받는 과정에서오류가 납니다 [1]
4833안종윤5/18/201711422winform을 Web Browser에 붙일 수 있는지요? [1]
4832guest5/17/201710910c# UDP socket예제 실행방법 [3]
4831guest5/15/201713388WPF .net 3.5에서 TextBox 한글 문제 [3]
4830초보자5/7/201718300프로그램 비정상 종료 메시지 창 없애는 방법 [4]
4829윤현수4/26/201714676System.Text.Encoding 질문 [1]
... 31  32  33  34  35  36  37  38  39  [40]  41  42  43  44  45  ...