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

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)
5019농상7/26/201813555오버플로우와 언더플로우 [2]
5018이재윤7/26/20189908정말 간단한 질문일 수 있는데요! [1]
5017나나7/24/201813419Stopwatch.ElapsedTicks와 Stopwatch.ElapsedMilliseconds [9]
5015지누7/18/201810394ASP.Net Core 를 안드로이드에서 작동하는게 가능한가요? [1]
5016지누7/19/20187537    답변글 [답변]: ASP.Net Core 를 안드로이드에서 작동하는게 가능한가요?
5014최홍준7/16/20187429Credential Provider Child Window 띄우기 [1]
5013Soul...7/16/20188054HtmlElement 스크롤 문의 [2]
5012황윤하7/13/20188591SMTP 예외처리 관련 질문 [1]
5011guest7/9/201816130c#에서 c++ DllImport 문의 입니다. [1]파일 다운로드2
5010C# 꿈...7/8/20188729C# WebService 에서 질문이 있습니다. [1]
5009C#초보자7/5/201810011[C#] 감시 프로그램을 제작했는데, 작동은 하나 폼이 멈춰버렵니다... [1]파일 다운로드1
5008도와주세요7/5/20189646 리스트 아이템의 타입을 추출해서 다시 재활용하고 싶어요.2 [6]파일 다운로드1
5007도와주세요7/4/201810757리스트 아이템의 타입을 추출해서 다시 재활용하고 싶어요. [1]
5006정환나라7/4/20189791소켓 통신과 쓰레드에 관해 질문드립니다 [4]
5005psh7/4/20188677인용에 관련해 문의 드립니다 [1]파일 다운로드2
5004까오리7/4/20188758iis8.0에서 닷넷1.1을 사용하기 위한 질문입니다. [1]
5002멋쟁이7/2/20189036WPFApp에 관한 초보 질문입니다. [1]파일 다운로드1
5001김학완7/1/20189231시작하세요 C# 7.1 프로그래밍 P53쪽 질문입니다. [2]
5000레몬6/26/201810899안녕하세요 성태님 도움으로 C# 네이버 카페 스팸글 작성되면 삭제되는 프로그램을 만들었는데요..여쭤볼게 하나 있습니다. [3]
4999jt6/21/20188765검색어 입력, 엔터 > 페북 로그인창으로 이동합니다. [4]
4998개발희망6/11/20189674C# 형변환 질문있습니다! [1]
4997초보개발자6/5/201821947C++ dll C#에서 사용하는데 보호된 메모리 오류떠요 한번만 도와주세요ㅜ [1]파일 다운로드1
4996swc6/5/201810167현업에서 주로 사용하는 DB업데이트 방법 질문입니다. [1]파일 다운로드1
4993학생6/3/20188364질문 드립니다 [1]
4991최진안5/24/20188338Credential Provider 질문 [2]
4990heyg...5/21/20189227Sybase Adaptive Server Anywhere 6 버전에 대한 질문입니다. [9]
... 31  32  33  [34]  35  36  37  38  39  40  41  42  43  44  45  ...