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

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)
4858heyh...7/10/201717966Clickonce update에 관한질문입니다. [1]
4857heyh...7/7/201718636제가 여태까지 작성한 보고서입니다. [2]파일 다운로드1
4856heyh...7/6/201716726성태님 다른질문입니다. [4]
4855JP7/6/201716931Dispose 패턴 구현시 Finalize 재정의에 대한 질문드립니다. [2]
4854heyh...7/6/201716879
4853heyh...7/5/201716470성태님이 작성한대로 해봤습니다. [1]파일 다운로드1
4852김레오7/4/201719466서드파티 dll 디버깅에 대해 질문드립니다. [2]
4851김현준7/3/201719955Datagridview VirtualMode 시 GC가 계속 호출되는 현상이 이해가 안갑니다. [2]
4850heyh...7/3/201718385성태님 밑에 질문드렸던 오류입니다. [1]파일 다운로드1
4849포플러7/2/201740210C#으로 만든 프로그램이 어느 순간 속도가 느려지거나 멈춤현상이 있습니다. [4]
4848윤진영7/2/201720528Microsoft Visual C++ 6.0 무설치 관련 [1]
4847heyh...6/30/201718155아무리 고민해도 답이 안나와서.. 질문 드립니다 [1]
4846heyh...6/28/201717733Clickonce에서 Clickonce로 변수 전달 [1]
4845heyh...6/26/201717208vb.net 에서 manifest 제거 방법 [1]파일 다운로드1
4844san6/21/201716862part3 pdf파일로 보는데 눈아퍼요.... 활자로 보고싶어요 [2]
4843윤현수6/20/201716069socket통신에 관한 질문입니다. [1]
4842오세운6/7/201717013로그인폼 다시 질문드려요. [1]
4841popo6/7/201717500궁금한사항이 있어 질문 드립니다. [1]
4840윤현수6/5/201717327datagridview에 바인딩시킨 dataset.table에 관련된 질문입니다. [2]파일 다운로드1
4839오세운6/2/201718324로그인폼 만들기 [1]
4838후배5/29/201719112PDF Reader 라이브러리.. [3]
4837KDP5/29/201718737c++ to C# 컨버팅 문의 [2]
4836popo5/26/201721332리플렉션 성능 관련 질문 드립니다. [2]
4835guest5/25/201717379wpf Canvas 질문드립니다. [1]
4834부탁드립...5/19/201718458익스체인지 서버 구동중 인증서를 받는 과정에서오류가 납니다 [1]
4833안종윤5/18/201718644winform을 Web Browser에 붙일 수 있는지요? [1]
... 31  32  33  34  35  36  37  38  39  40  [41]  42  43  44  45  ...