본문 바로가기

   
Programming/C#

프로퍼티(Properti), 인덱서(indexer)

반응형

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Test

{

       class Property01

       {

             static void Main(string[] args)

             {

                    Test t1 = new Test(10);

 

                    Console.WriteLine(t1.GetN());

                    Console.WriteLine(t1.ToString());

 

                    t1.SetN(20);

                    Console.WriteLine(t1.GetN());

 

                    //t1.n = 30;

                    //Console.WriteLine(t1.n);

 

                    //

                    t1.N = 300;

                    Console.WriteLine(t1.N);

             }

       }

 

       class Test

       {

             //1~100 캡슐화

             private int n;

 

             //프로퍼티 선언:내부적으로는 메소드 외부적으로는 변수

             //1. 인자리스트가 없음.

             //2. 이름이 멤버변수와 동일

             public int N

             {

                    set

                    {

                           if (value >= 1 && value <= 100)

                                 this.n = value;

                    }

 

                    get

                    {

                           return this.n;

                    }

             }

 

             //기본 생성자 재정의

             public Test()

             {

                    this.n = 1;

             }

 

             //생성자 오버로딩

             public Test(int n)

             {

                    if (n >= 1 && n <= 100)

                           this.n = n;

                    else

                           this.n = 1;

             }

 

             //Get, Set 메서드

             public void SetN(int n)

             {

                    if (n >= 1 && n <= 100)

                           this.n = n;

             }

 

             public int GetN()

             {

                    return this.n;

             }

 

             //ToString() 오버라이딩(재정의)

             //리턴을 스트링 형으로 반환 해야함

             public override string ToString()

             {

                    return this.n.ToString();

             }

       }

}

 


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Test

{

       class Property02

       {

             static void Main(string[] args)

             {

                    Student s1 = new Student(100, 90, 80, 1980);

 

                    //읽기, 쓰기

                    s1.Kor = 95;

                    Console.WriteLine(s1.Kor);

 

                    //쓰기 전용

                    s1.Eng = 85;

                    //Console.WriteLine(s1.Eng);

 

                    //읽기 전용

                    //s1.Math = 76;

                    Console.WriteLine(s1.Math);

 

                    Console.WriteLine(s1.Age);

 

                    Console.WriteLine(Student.School);

 

                    Student.School = "동대문중학교";

                    Console.WriteLine(Student.School);

             }

       }

 

       class Student

       {

             private int kor;

             private int eng;

             private int math;

             private int birthYear;

             private static string school;

 

             //객체 생성자

             public Student(int kor, int eng, int math, int birthYear)

             {

                    this.kor = kor;

                    this.eng = eng;

                    this.math = math;

                    this.birthYear = birthYear;

             }

 

             //정적 생성자 - 오버로딩 불가능 유일하다 단1

             static Student()

             {

                    Student.school = "당산 중학교";

             }

 

             //프로퍼티

             //1. 읽기, 쓰기 가능 속성

             public int Kor

             {

                    set { this.kor = value; }

                    get { return this.kor; }

 

             }

 

             //2. 쓰기 전용 속성       

             //public virtual SetEng(int value) {}

             public int Eng

             {

                    set { this.eng = value; }

             }

 

             //3. 읽기 전용 속성

             public int Math

             {

                    get { return this.math; }

             }

 

             //4. 가상 속성

             public int Age

             {

                    get

                    {

                           return DateTime.Now.Year - this.birthYear;

                    }

             }

 

             //5. 정적 송석

             //   - 정적 변수를 접근

             public static string School

             {

                    set

                    {

                           Student.school = value;

                    }

 

                    get

                    {

                           return Student.school;

                    }

             }

       }

}

 



인덱서(Indexer)

 - 프로퍼티의 일종 -> 메서드의 일종

 - 객체가 내부에 배열멤버를 가졌을떄..

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Test

{

       class Indexer01

       {

             static void Main(string[] args)

             {

                    //인덱서 문자열

                    string str = "홍길동";

                    //읽기 전용 str[0] = '';

                    Console.WriteLine(str[0]);

                    Test t = new Test();

 

                    //int[] temp = t.GetNums();

                    //temp[0] = 100;

 

                    Console.WriteLine(t[0]);

                    t[0] = 90;

                    Console.WriteLine(t[0]);

 

                    Time time = new Time();

                    Console.WriteLine(time[""]);

                   

             }

       }

 

       class Time

       {

             //배열이 없어도 인덱서는 구현 가능

             //배열은 숫자인덱스와 문자 인덱스를 가질수 있다. t[0](스칼라배열), t['A'](연관배열)

             public string this[string n]

             {

                    get

                    {

                           if (n == "하나")

                                 return "one";

                           else if (n == "")

                                 return "two";

                           else if (n == "")

                                 return "three";

                           else

                                 return "zero";

                    }

                    set

                    {

                    }

             }

       }

 

       //참조변수에 주소를 가져오면 모든것을 다할수 있다.

       //메소드의 반환타입은 값 형태로 돌려주어야한다. 안정성 상승 (캡슐화가 깨진다.)

       class Test

       {

             private int[] nums;

             private int[,] nums2;

 

             public Test()

             {

                    this.nums = new int[5] { 10, 20, 30, 40, 50};

             }

 

             public int GetNums(int index)

             {

                    return this.nums[index];

             }

 

             //인덱서(클래스의 멤버) : 클래스 객체를 배열 형태로 사용할수 있도록 해준다.

             //1. 이름은 반드시 this(객체 자신)

             //2. 인덱스는 인자리스트를 갖는다.

 

             //t1[0] = 90;

             //Console.WriteLine(t1[0]);

             public int this[int n]

             {

                    //내부는 프로퍼티와 동일

                    set

                    {

                           this.nums[n] = value;

                    }

 

                    get

                    {

                           return this.nums[n];

                    }

             }

       //3. 인덱서는 오버로딩 가능 (인자의 갯수와 타입 다르게)

             public int this[int n, int m]

             {

                    set

                    {

                           this.nums2[n, m] = value;

                    }

 

                    get

                    {

                           return this.nums2[n, m];

                    }

             }

       }

 

 

 

       //public int this[int n]

      

}

 


 

반응형