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

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)
5072엔벌잉10/24/20188033C# textbox, button질문입니다! [4]파일 다운로드1
5071엔벌잉10/23/20188410C#윈도우폼 질문입니다!! [2]
5070진우10/17/20188358Visual Studio 서비스팩과 업데이트 차이 문의 [2]
5069감자10/12/20189090빌드 구성을 재설정하는 방법이 있을까요? [1]파일 다운로드1
5068누오10/10/20187857ASP Core 2.0 에서 dll안에 있는 뷰 읽어들이는 방법? [1]
5067김정민10/5/20187868다른 윈도우가 깨지는 현상을 막을 수 있을까요 [3]
5066로니브10/4/20188651ASP.NET MVC에서 View 파일 숨기는법? 보안처리 하는법? 관련 질문.. [1]
5065키모10/1/20189863문자 질문입니다. [3]
5064로니브10/1/20189189클래스 라이브러리에서 .cshtml파일을 추가하는 방법은 없나요? [3]
5063진우9/28/20188235ADO.net 과 Entity Framework 차이 문의 [2]
5062테스트9/27/20188767C# import file 의 구조체 배열 선언 및 호출에 대해 문의. [3]
5061안녕하세요9/13/20188741c# 프로그래밍 관련 문의 [1]
5060임민재9/8/20188227c# install 파일 생성 시 문제가 발생하였습니다 [1]파일 다운로드1
50599/7/20187890Winform TextBox 포커스 유지하는 방법 질문 [파일첨부] [1]파일 다운로드1
50589/5/201811297Winform TextBox 포커스 유지하는 방법 질문 [3]
5056박종윤8/30/201810635c# dll을 C++에서 사용 시 event 호출 [4]파일 다운로드1
5055초보자8/29/20189281asp.net 에서 다른 서버의 iis를 stop하는 batch file을 실행시키는데 동작하지 않습니다. [5]
5054사도신8/29/20187975[wpf] textbox insert overite 모드시에 [4]파일 다운로드1
5053엿장수8/26/20187333directshow filter 에서의 IMediaSample 의 시간에대한질문입니다 [1]
5052오명현8/26/20188231Tcp소켓 실습 Exeption 도와주세요! [4]파일 다운로드1
5049오명현8/23/20187590책 477페이지 내용 중 이해가 안가는 부분이 있어 질문드립니다. [1]
5048오명현8/23/20187281포트 관련 질문 하나더 있습니다. [1]
5047오명현8/22/20187950포트가 없을 경우를 가정한 내용에 대해 질문이 있습니다. 책468p. [1]
5046엿장수8/22/20187842다이렉트쇼 필터 추가하는데 [2]
5045임도진8/22/20188561c# opencv dll파일 로드 질문 [3]파일 다운로드1
5044엿장수8/20/20188259graphedit 에 등록되어있는 필터를 가져와서 사용하는방법을 알고싶습니다 [2]
... 31  [32]  33  34  35  36  37  38  39  40  41  42  43  44  45  ...