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

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)
4909필승11/5/201710622TextBox에 관해 질문 드립니다. [5]
4908필승11/1/201713956특정 시각에 프로그램 종료 또는 재시작시 오류 발생 [8]
4907심너울10/29/20179554선생님이 쓰신 "시작하세요! C# 7.1~"으로 공부하고 있는데요~ [2]
4906guest10/25/20179117.net core, .net standard 관련해서 궁금합니다 [2]
490510/25/20178396watermark 관련문의 [1]
4903Ho10/25/20178788간단한 서버를 구현해 보고 싶습니다. [2]
4902황재승10/22/20179835C# 7.1 프로그래밍 왕초보자도 이해할 수 있는 수준인가요? [1]
4901김레오10/17/201714537c# 프로그램 난독화 도구인 confuserex라는 도구를 사용하다 여쭙습니다. [2]
4900황준범10/12/201710302ClickOnce 배포관련 질문드립니다. [1]
4899Ques...9/28/201712754C# 프로그램이 "응답 없음" 시에도 계속 독립적으로 돌아가는 타이머 생성법 [3]
4898ssdrm9/27/20179539Clickonce 실행시 보안에 막힙니다 ㅠ [1]
4897김태진9/23/20179702윈도우7 작업관리자의 상세(details)탭 생성에 대해 여쭙습니다. [1]
4896장진국9/19/201711070안녕하세요 WPF 에서 Window객체가 가비지 콜렉션에 의해 수집되지 않는거 같아서 문의드립니다. [1]
4895Ques...9/18/201710724Generic 에 관하여 질문드립니다. [5]
4894얼마전6...9/14/201712622C# 7.1에서 보강된 부분만 PDF로 제공하는 건 아니되나요? [2]
4893BigII9/14/201711651타 언어(JAVA, PHP 등)에서 받은 RSA 개인키 문자열을 이용하여 내용 복호화 가능 여부 [4]
4892Ques...9/13/201710704서브 폼에서는 무거운 작업을해도 속도가 빠를까요 ?? [1]
4891윤현수9/11/201711307Taskbar에 관한 질문입니다. [5]
4890제발9/11/20179833 시작하세요! C# 6.0 프로그래밍 책이 절판됐나요? [5]파일 다운로드1
4889낙낙이9/7/20179269안녕하세요! xsl관련 이야기입니다. [1]
4888heyh...9/6/20179093클릭원스를 수동으로 배포 시 업데이트 할 때 [1]
4887이경현9/4/20179640Windows server 2012 파일 없어짐 현상... [1]
4886질문자9/1/201711518disconnecteditem에 대하여 아시나요? [4]파일 다운로드1
4883솔솔8/30/201710028dataview에서 select한 index 가져오기 [1]
4882user8/30/201712656UI 변경 작업 여러개를 동시에 사용하려면 어떻게 해야되나요 ?? [7]
4884user8/31/20179306    답변글 [답변]: UI 변경 작업 여러개를 동시에 사용하려면 어떻게 해야되나요 ??파일 다운로드1
... 31  32  33  34  35  36  37  [38]  39  40  41  42  43  44  45  ...