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

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)
4884user8/31/20179312    답변글 [답변]: UI 변경 작업 여러개를 동시에 사용하려면 어떻게 해야되나요 ??파일 다운로드1
4881kmi8/30/20179495진행속도가 중간에 더뎌지는 문제가 있는데 해결할 수 있는 방법이 있나 궁금합니다. [3]
4880김호종8/29/20179907HTTP JSON POST 관련 질문 드립니다. [1]
4879aos8/27/201710077DateTime 표시할 때 포맷 설정시 밀리초까지 포함하는 법이 궁금합니다 [1]
4878heyh...8/25/201710168프로세스의 프로세스 찾기(?) [2]
4877강준8/24/20179358SQLite journal_mode=wal 관련하여 질문드립니다. [1]
4876heyh...8/23/20179610프로세스 초기화하기 [1]
4874ho8/22/20179677파일 확장자명을 이용해 파일의 실행 프로그램의 전체 경로를 얻어 올 수 있을까요? [1]
4875ho8/23/201710635    답변글 [답변]: 파일 확장자명을 이용해 파일의 실행 프로그램의 전체 경로를 얻어 올 수 있을까요? [1]
4873kmi8/21/201711472전역 변수를 쓰지 않고 여러 군데에서 같은 변수를 공용하는 방법이 궁금합니다. [4]
4872abcd8/18/201710157프로세스를 초기화 하는 명령어도 있나요? [1]
4871kmi8/17/201711468메모리 부족으로 종료되는 현상의 여러가지 이유가 무엇인지 궁금합니다. [2]
4870heyh...8/14/201710268프로그램 실행시 중복일 때 버튼 색깔 원래대로 돌리기 [1]
4868kmi8/4/20179587string[] 에 Reverse 적용방법 질문해봅니다 [3]
4867heyh...8/4/20179074EventHandler에 관한 [1]
486610년차8/3/20179504dsoframer axframer open시 기존 오픈되어있는 엑셀을 먹어버리는 현상 [1]
4865heyh...7/31/201711062클릭원스로 배포 한 프로젝트가 끝났는지 알 수 있는 방법 [8]
4864초보자7/28/201710070DllIImport질문 드립니다. [1]
4863다연아빠7/23/201710283전역 예외처리에 대해 질문있습니다. [3]
4861라르크7/17/201715555window form 예제 따라하는 중인데 12.3 서비스 응용 프로그램에서 진행이 안됩니다. [3]파일 다운로드1
4859heyh...7/10/201710201다른 환경에서 실행하기 [1]
4858heyh...7/10/201710043Clickonce update에 관한질문입니다. [1]
4857heyh...7/7/201710675제가 여태까지 작성한 보고서입니다. [2]파일 다운로드1
4856heyh...7/6/20179389성태님 다른질문입니다. [4]
4855JP7/6/201710082Dispose 패턴 구현시 Finalize 재정의에 대한 질문드립니다. [2]
4854heyh...7/6/20179511
... 31  32  33  34  35  36  37  38  [39]  40  41  42  43  44  45  ...