¼Ò¼³°°ÀºC#  Á¦5°­ÀÇ C#ÀÇ ´ÙÇü¼º  12. ÀÎÅÍÆäÀ̽º ³»ÀÇ ¼Ó¼º ±¸Çö  

 

5.12 ÀÎÅÍÆäÀ̽º ³»ÀÇ ¼Ó¼º ±¸Çö

 

¾Õ¿¡¼­´Â ÀÎÅÍÆäÀ̽ºÀÇ ¸â¹ö·Î ¸Þ¼­µå¸¦ ±¸ÇöÇÏ´Â ¿¹Á¦¸¦ »ìÆì º¸¾Ò½À´Ï´Ù. À̹ø¿¡´Â ¼Ó¼º(Property)À» ¸â¹ö·Î ÇÏ´Â ÀÎÅÍÆäÀ̽ºÀÇ ±¸Çö¿¡ ´ëÇØ ¾Ë¾Æ º¸·Á ÇÕ´Ï´Ù. ¼Ó¼ºÀÌ ÀÎÅÍÆäÀ̽º¿¡ »ç¿ëµÉ ¶§¿¡´Â ¼Ó¼ºÀÇ À̸§°ú setÀ» ±¸ÇöÇÒ °ÍÀÌÁö getÀ» ±¸ÇöÇÒ °ÍÀÎÁö¸¸ ¸í½ÃÇÏ¸é µË´Ï´Ù. ´ÙÀ½Àº ¼Ó¼ºÀ» ÀÎÅÍÆäÀ̽º¿¡¼­ »ç¿ëÇϰí À̸¦ ±¸ÇöÇÑ ¿¹Á¦ÀÔ´Ï´Ù.

 

&

ImplementsClassMain.cs

Ü ¼Ó¼ºÀ» ¸â¹ö·Î ÇÏ´Â ÀÎÅÍÆäÀ̽º ±¸Çö ¿¹Á¦  

using System;

interface IMyInterface {

   int IProperty {

      get;

      set;

   }

}

class ImplementsClass : IMyInterface {

   private int myValue;

   public int IProperty {

      get {

         return myValue;

      }

      set {

         myValue = value;

      }

   }

}

class ImplementsClassMain {

   public static void Main() {

      ImplementsClass ic = new ImplementsClass();

      IMyInterface imi = ic;

                              imi.IProperty = 1000;

                              Console.WriteLine("InterfaceÀÇ Property : " + imi.IProperty);

                           }//main

}//class

C:\C#Example\05>csc ImplementsClassMain.cs

C:\C#Example\05>ImplementsClassMain

InterfaceÀÇ Property : 1000

 

¿ì¼± ¸öü ¾ø´Â ¼Ó¼ºÀ» ¸â¹ö·Î °®´Â ÀÎÅÍÆäÀ̽º¸¦ ¼±¾ðÇÕ´Ï´Ù. set°ú getÀº ¼Ó¼ºÀÇ °ª¿¡ Á¢±ÙÇÒ ¶§ ¾²´Â Á¢±ÙÀÚ·Î get Á¢±ÙÀÚ´Â ¼Ó¼º ¸â¹öÀÇ °ªÀ» ÀÐÀ» ¶§ »ç¿ëµÇ¸ç, set Á¢±ÙÀÚ´Â ¼Ó¼º ¸â¹ö¿¡ °ªÀ» ÀúÀåÇÒ ¶§ È£ÃâµË´Ï´Ù. ÀÌ Á¢±ÙÀÚµéÀº ±¸ÇöµÇÁö ¾Ê°í ´Ü¼øÈ÷ ¼±¾ð¸¸ µÇ¾î ÀÖ½À´Ï´Ù. ÀÎÅÍÆäÀ̽º¿¡¼­ ¼Ó¼ºÀ» ¼±¾ðÇÏ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°½À´Ï´Ù..

 

n                 interface IMyInterface {

n                    int IProperty {

n                       get;

n                       set;

n                    }

n                 }

 

¸Þ¼­µå³ª ¼Ó¼º ¿Ü¿¡µµ ÀÎÅÍÆäÀ̽ºÀÇ ¸â¹ö·Î µÉ ¼ö ÀÖ´Â °Í¿¡´Â Àε¦¼­(Indexer)¿Í À̺¥Æ®(Event)µµ ÀÖ½À´Ï´Ù.



jabookÀúÀÚ¸íÇÔ
Á¦¸ñ:¼Ò¼³°°ÀºC# Á¦5°­ÀÇ
ÀúÀÚ:ÃÖ¿µ°ü