안녕하세요, Davey 입니다. 오늘 Posting할 사항은, C# 에서 선언하는 변수와 메소드의 접근 허용 범위를 정의하는 접근 한정자 개념과, 클래스 간 형식 변환 그리고, 마지막으로 is 와 as 의 개념에 대한 내용입니다. 하나 하나 설명해 드리겠습니다.
변수 접근 한정자 개념
: 위에서 설명 드렸듯이, C# 에서 선언하는 변수와 메소드에는 접근 허용 범위를 정의하는데, 그 정의하는 구문을 접근 한정자라고 합니다. 접근 한정장의 종류는 아래와 같습니다.
한정자 Type | 한정자 Type 별 설명 (Description) |
public | 선언하는 class 내부 및 외부에서 모두 사용이 가능하도록 허용함 |
private | |
internal | |
protected | |
protected internal | 1) 같은 어셈블리 : protected로 접근 가능하도록 허용 2) 다른 어셈블리 : private와 같은 개념으로 접근 가능하도록 허용 |
private protected |
클래스 간 형식 변환
: 클래스 간 형식을 변환하는 구문이며, 기반과 파생 클래스 연결 시 형식 변환이 가능합니다. 즉, 기반 클래스로 인스턴스를 생성 후, 파생 클래스 형식으로 변환이 가능하다고 생각하시면 됩니다. 그 반대로는 불가능 한걸로 알고 있습니다.
- 이해를 돕기 위해서 아래와 같이 클래스 간 형식 변환 예시 code를 작성하였습니다.
================================================================
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { class grandfather { public void say_hello() { Console.WriteLine("차 조심해라"); } } class father : grandfather // 기반 클래스 grandfather 연결 해줌 { public void say_hello1() { Console.WriteLine("오래 오래 건강하게 사세요"); } } class son : father // 기반 클래스 father 연결 해줌 { public void say_hello2() { Console.WriteLine("아이폰 사주세요"); } } class Main_Class { static void Main(string[] args) { grandfather test1 = new grandfather(); // grandfather class로 test1 인스턴스 하나 생성 test1.say_hello(); // grandfather1 class 내부 메소드 호출 father test2 = new father(); // father class로 test2 인스턴스 하나 생성 test2.say_hello(); // 기반 클래스인, grandfather class 내부 메소드 호출 test2.say_hello1(); // father class 내부 메소드 호출 son test3 = new son(); // son class로 test3 인스턴스 하나 생성 test3.say_hello(); // 기반 클래스인, grandfather class 내부 메소드 호출 test3.say_hello2(); // son class 내부 메소드 호출 Console.WriteLine("\n" + "변환 후 결과 값입니다."); test1 = new father(); father test4 = (father)test1; // grandfather class로 선언한 test1 인스턴스의 형식을 father 로 변환 test4.say_hello(); // 변환 후 grandfather class 메소드 출력 test4.say_hello1(); // 변환 후 father class 메소드출력 test1 = new son(); son test5 = (son)test1; // grandfather class로 선언한 test1 인스턴스의 형식을 son 로 변환 test5.say_hello(); // 변환 grandfather class 메소드 출력 test5.say_hello1(); // 변환 father class 메소드 출력 test5.say_hello2(); // 변환 son class 메소드 출력 Console.ReadKey(); } } }
================================================================
- Run을 하게 되면 아래와 같이 결과 값을 보실 수 있습니다. 일단, grandfather로 선언한 인스턴스를 father과 son class type으로 변환하는 과정이라고 이해하시면 될 거 같습니다.
is 와 as
1) is : class type에 대해서 검증하는 bool 구문이라고 이해하시면 됩니다.
2) as : class type 변환 구문입니다. 위에 설명드린 클래스 간 형식 변환과 비슷하다고 생각하시면 됩니다.
- 쉬운 이해를 돕기 위해, 아래와 같이 is 와 as 예시 code를 작성하였습니다.위2번항목code도같이합쳐서보여드리겠습니다.
================================================================
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { class grandfather { public void say_hello() { Console.WriteLine("차 조심해라"); } } class father : grandfather { public void say_hello1() { Console.WriteLine("오래 오래 건강하게 사세요"); } } class son : father { public void say_hello2() { Console.WriteLine("아이폰 사주세요"); } } class Main_Class { static void Main(string[] args) { grandfather test1 = new grandfather(); // grandfather class로 test1 인스턴스 하나 생성 test1.say_hello(); // grandfather1 class 내부 메소드 호출 father test2 = new father(); // father class로 test2 인스턴스 하나 생성 test2.say_hello(); // 기반 클래스인, grandfather class 내부 메소드 호출 test2.say_hello1(); // father class 내부 메소드 호출 son test3 = new son(); // son class로 test3 인스턴스 하나 생성 test3.say_hello(); // 기반 클래스인, grandfather class 내부 메소드 호출 test3.say_hello2(); // son class 내부 메소드 호출 Console.WriteLine("\n" + "변환 후 결과 값입니다."); test1 = new father(); father test4 = (father)test1; // grandfather class로 선언한 test1 인스턴스의 형식을 father 로 변환 test4.say_hello(); // 변환 후 grandfather class 메소드 출력 test4.say_hello1(); // 변환 후 father class 메소드출력 test1 = new son(); son test5 = (son)test1; // grandfather class로 선언한 test1 인스턴스의 형식을 son 로 변환 test5.say_hello(); // 변환 grandfather class 메소드 출력 test5.say_hello1(); // 변환 father class 메소드 출력 test5.say_hello2(); // 변환 son class 메소드 출력 Console.WriteLine("\n" + "is & as 예시 Code 결과 값 입니다."); grandfather test6 = new father(); test6.say_hello(); father test7 = test6 as father; test7.say_hello(); test7.say_hello1(); if (test6 is grandfather) // test6 인스턴스의 Type이 grandfather 인지에 대한 검토 { Console.WriteLine("grandfather class Type 입니다."); } if (test7 is father) // test7 인스턴스의 Type이 father 인지에 대한 검토 { Console.WriteLine("father class Type 입니다."); } else { Console.WriteLine("Type 변환이 이뤄지지 않았습니다."); } Console.ReadKey(); } } }
================================================================
- Run 해보시면 아래와 같은 결과 값을 보실 수 있습니다. 즉, as 구문을 통해서, grandfather type인 test6를 father type으로 변환하였고, 성공적으로 변환이 되었기에, is로 검증 결과 위와 같은 결과 값을 출력이 가능합니다.
이상입니다. 지금까지 접근 한정자 개념, 클래스 형식 변화 그리고 is 와 as 개념에 대해서 포스팅을 작성하였습니다. 변수 및 메소드 한정자 Type에 대해서 어떤 분들은 public 다 쓰면 되는 거 아니야 라고 애기하시는데, 변수 선언을 할 때, 너무 많은 변수를 남용함으로써, 나중에 code 유지 보수가 정말 어려워지는 것을 감안하면, 적당한 한정 범위를 두는 것을 추천 드립니다.
사실 저도 간단한 code는 다 public으로 할 때도 많습니다. (개인적인 취향이 좀 들어가는 거 같습니다.) 그리고, 클래스 형식 변환 및 형식 체크를 위해 사용하는 is 와 형식 변환을 위해 사용하는, 또 다른 문구인 as에 대해서도 알아 두시면 유용하실 듯 합니다. 이번 posting도 조금이나마 도움이 되셨으면 하네요! C# 프로그래머가 되기 위해서 같이 노력하시죠! 감사합니다.
[저작권이나, 권리를 침해한 사항이 있으면 언제든지 Comment 부탁 드립니다. 검토 후 수정 및 삭제 조치 하도록 하겠습니다. 그리고, 기재되는 내용은 개인적으로 습득한 내용이므로, 혹 오류가 발생할 수 있을 가능성이 있으므로, 기재된 내용은 참조용으로만 봐주시길 바랍니다. 게시물에, 오류가 있을때도, Comment 달아 주시면, 검증 결과를 통해, 수정하도록 하겠습니다.]
'C# > C# 일반 & Basic (General & Basic)' 카테고리의 다른 글
C# 프로그래머 되기 추상 클래스 및 Property 기본 구문 (0) | 2021.03.04 |
---|---|
C# 프로그래머 되기 오버라이딩 사용 및 봉인, 메소드 숨기기 (0) | 2021.03.04 |
C# 프로그래머 되기 기반 클래스, 파생 클래스, 그리고 this 개념 (0) | 2021.03.04 |
C# 프로그래머 되기 - ArrayList 활용하기 (1) | 2021.03.04 |
C# 프로그래머 되기 - 클래스 Class 란 (0) | 2021.03.03 |
댓글