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

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]

... 46  47  48  49  50  51  [52]  53  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1526황상대1/26/201513259C# 마샬링 관련 질문이 있습니다. [1]
1525Ji-y...1/19/201511862시작하세요 C# 프로그래밍 도서에 관한 질문입니다. [1]
1523임종복12/31/201419253windows 2008 r2에서 ms-sql 2008 서버사용시 tls문제점 [3]
1524임종복1/4/201514139    답변글 [답변]: windows 2008 r2에서 ms-sql 2008 서버사용시 tls문제점 [2]
1521윤대욱12/19/201411661C++ 에서 서버와 클라이언트를 나누어서 구조체 전달에 대해서 궁금합니다. [1]파일 다운로드1
152012/10/201412010datetime관련해서요 [1]
1516aTo12/1/201411475wpf 배포관련. [3]
1514얄미운제리11/17/201412742[C# Mono]Mono Chart 개발 문의드립니다. [1]
1355미나리나물11/13/201410284RDP 접속 방식 질문드리고 싶습니다. [1]
1353송용국11/10/20149448TeamFoundationServer 에 소스를 Target서버로 옮기는(내려주는) 방법 있나요? [1]
1351blue...11/7/201414251c# webbrowser 모바일환경 접속 [1]
1350에메11/7/201413724WCF namedpipe 퍼블리싱 충돌 문제 [3]파일 다운로드1
1349Chun...11/6/201418556Microsoft. Net Framework 액세스 거부 오류...... [2]
1348김준석11/6/201414389 Mini dump 기록 안되는 현상 [2]파일 다운로드1
1346개미11/5/201428799https페이지에서 http서버에 있는 이미지 파일 호출시 보안 오류 [8]
1345장구니11/4/201412320AWS 인스턴스를 만들었는데 [1]
1343h11/2/201412203안녕하세요 [1]
1342왕초보11/1/201417232C#에서의 함수포인터 [13]
1347왕초보11/5/201412396    답변글 [답변]: C#에서의 함수포인터 [1]
1341영택10/29/201413200MFC Amazon S3 사용관련하여 질문드립니다. [2]
1344영택11/4/20149930    답변글 [답변]: MFC Amazon S3 사용관련하여 질문드립니다. [2]
1340감사합니다10/28/201414345c# wpf shdocvw internet explorer를 윈도우프레임으로 집어 넣을 수 있는지 궁금합니다. [3]파일 다운로드1
1338헬프미10/28/201415286tcp 소켓의 SYN_RECV 상태에 대하여 질문있습니다. [3]
1336이소정10/27/201410802filestream seek에 대해 질문있습니다. [1]
1337이소정10/28/201410790    답변글 [답변]: filestream seek에 대해 질문있습니다.파일 다운로드1
1339이소정10/28/201413696        답변글 [답변]: [답변]: filestream seek에 대해 질문있습니다. [2]파일 다운로드2
... 46  47  48  49  50  51  [52]  53  54  55  56  57  58  59  60  ...