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

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)
1306이상현8/7/201410057Entity 프레임웍에서 테이블만 추가로 등록하는 방법
1307이상현8/7/201410364    답변글 [답변]: 자답이네요. [1]
1305popo8/7/201411629wpf UserControl List 바인딩 질문 입니다. [1]
1304민경수8/7/201414207네이버 카페에 구글 스프레드 시트 삽입 [1]
1303김동진8/7/201411411vmware환경에서 Guest IP를 192대역으로 받을 수 있는 방법은 없을까요~? [3]
1302김문경7/31/201410582스마트클라이언트 오라클 연결 [1]
1314이재균8/20/201410614    답변글 [답변]: 스마트클라이언트 오라클 연결
1300아로스7/28/201423467c# 인터넷 임시 파일 삭제 문의 [12]파일 다운로드1
1299서동원7/28/201412076안녕하세요. 혹시 Internet_Zone과 관련된문제인가 해서 질문드립니다. [1]
1298(non...7/25/201417469(글쓴이의 요청으로 삭제합니다.) [15]
1295서동원7/22/201413171안녕하세요. 스마트클라이언트에 대해 질문드립니다. [2]파일 다운로드1
1294(non...7/20/201411546(글쓴이의 요청으로 삭제합니다.) [2]
1293VS20...7/20/201412151VS2013 Ultimate에 Windows Phone 프로젝트 템플릿 추가 방법 문의 [1]파일 다운로드2
1292(non...7/20/201410870(글쓴이의 요청으로 삭제합니다.) [2]
1291zino7/16/201411476chromium 배포본 만들기~ [1]
1290(non...7/13/201412633(글쓴이의 요청으로 삭제합니다.) [3]
1288박주만7/8/201421802C# 서비스 기반 데이터베이스(mdf) & InstallShield Limited Edition 설치 및 배포 [2]파일 다운로드1
1287김용환7/8/201419822오라클 db 사용관련 문의입니다. [4]파일 다운로드1
1286C#조으다7/8/201410783WebBrowser 공유기 관리 웹 페이지 인증 [3]
1285C#조으다7/5/201411126IE DocumentComplete 이벤트가 발생되지 않습니다. [2]
1284(non...7/4/201411263(글쓴이의 요청으로 삭제합니다.) [3]
1283김영대7/3/201414382안녕하십니까 정성태님 죄송하지만 SmartClient 에 관한 질문이 있습니다. [9]
1282(non...7/2/201411268(글쓴이의 요청으로 삭제합니다.) [2]
1281(non...7/1/201412357(글쓴이의 요청으로 삭제합니다.) [4]
1280동동이6/25/201411575안녕하세요. ocx의 비동기 또는 쓰레드에서 호출 [1]
1279(non...6/23/201411642(글쓴이의 요청으로 삭제합니다.) [17]
... 46  47  48  49  50  51  52  53  [54]  55  56  57  58  59  60  ...