¼Ò¼³°°ÀºC#  Á¦5°­ÀÇ C#ÀÇ ´ÙÇü¼º  18. ¾÷ij½ºÆÃ(Upcasting)ÀÇ ±¸Çö  

 

5.18 ¾÷ij½ºÆÃ(Upcasting)ÀÇ ±¸Çö

 

¾÷ij½ºÆÃÀÇ ½ÇÁ¦ÀûÀÎ ±¸ÇöÀÇ ¿¹¸¦ ¾Ë¾Æ º¸µµ·Ï ÇϰڽÀ´Ï´Ù. ¾Õ¿¡¼­ ¹è¿î °ÍµéÀ» °¡Áö°í ¾Æ·¡ÀÇ ¾÷ij½ºÆÃ(Upcasting) ¿¹Á¦¸¦ Å×½ºÆ®ÇØ º¸½Ã±â ¹Ù¶ø´Ï´Ù.

 

&

UpcastingClassTest.cs

Ü UpcastingÀ» Å×½ºÆ®ÇÏ´Â ¿¹Á¦  

using System;

class Shape {

      public virtual void Draw() {

               Console.WriteLine("¸ð¾çÀ» ±×¸³´Ï´Ù");

      }

      public virtual void Delete() {

               Console.WriteLine("¸ð¾çÀ» Áö¿ó´Ï´Ù");

      }

}

class Circle : Shape  {

      public override void Draw() {

               Console.WriteLine("¿øÀ» ±×¸³´Ï´Ù");                    

      }

      public override void Delete() {

               Console.WriteLine("¿øÀ» Áö¿ó´Ï´Ù");                        

      }

      public void SayCircle() {

               Console.WriteLine("¾È³çÇϼ¼¿ä ¿øÀÔ´Ï´Ù");

      }

}

class Triangle : Shape                            {

      public override void Draw() {

               Console.WriteLine("»ï°¢ÇüÀ» Çϳª, µÑ, ¼Â, ±×¸³´Ï´Ù.");          

      }

      public override void Delete() {

               Console.WriteLine("»ï°¢ÇüÀ» Áö¿ó´Ï´Ù");              

      }

      public void SayTriangle() {

               Console.WriteLine("¾È³çÇϼ¼¿ä »ï°¢ÇüÀÔ´Ï´Ù");

      }

}

class Rectangle : Shape {

      public override void Draw() {

               Console.WriteLine("»ç°¢ÇüÀ» ¿ø, Åõ, ¾²¸®, Æ÷ ±×¸³´Ï´Ù.");                        

      }

      public override void Delete() {

               Console.WriteLine("»ç°¢ÇüÀ» Áö¿ó´Ï´Ù");              

      }

      public void SayRect(){

               Console.WriteLine("¾È³çÇϼ¼¿ä »ç°¢ÇüÀÔ´Ï´Ù");

      }

}

//abstractÀ» Å×½ºÆ®Çϱâ À§ÇÑ Å¬·¡½º

public class UpcastingClassTest {

          public static void Main() {

           Shape s = new Shape();

           s.Draw();

           s.Delete();

           Console.WriteLine();

               //Ŭ·¡½ºÀÇ ±âº»ÀûÀÎ »ç¿ë¹ý

           Circle c = new Circle();

           c.Draw();

           c.Delete();

           c.SayCircle();

           Console.WriteLine();

           Triangle t = new Triangle();

           t.Draw();

           t.Delete();

           t.SayTriangle();

           Console.WriteLine();

           Rectangle r = new Rectangle();

           r.Draw();

           r.Delete();

           r.SayRect();

           Console.WriteLine();

               //UpcastingÀÇ »ç¿ë

           Shape c1 = new Circle();

           c1.Draw();

           c1.Delete();

               //c1.SayCircle(); ¿¡·¯°¡ ¹ß»ýÇÕ´Ï´Ù.

           Console.WriteLine();

           Shape t1 = new Triangle();

           t1.Draw();

           t1.Delete();

               //t1.SayTriangle(); ¿¡·¯°¡ ¹ß»ýÇÕ´Ï´Ù.

           Console.WriteLine();

           Shape r1 = new Rectangle();

           r1.Draw();

           r1.Delete();

               //r1.SayRect(); ¿¡·¯°¡ ¹ß»ýÇÕ´Ï´Ù.

      }//main

}//class

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

C:\C#Example\05>UpcastingClassTest

¸ð¾çÀ» ±×¸³´Ï´Ù

¸ð¾çÀ» Áö¿ó´Ï´Ù

 

¿øÀ» ±×¸³´Ï´Ù

¿øÀ» Áö¿ó´Ï´Ù

¾È³çÇϼ¼¿ä ¿øÀÔ´Ï´Ù

»ï°¢ÇüÀ» Çϳª, µÑ, ¼Â, ±×¸³´Ï´Ù.

»ï°¢ÇüÀ» Áö¿ó´Ï´Ù

¾È³çÇϼ¼¿ä »ï°¢ÇüÀÔ´Ï´Ù

 

»ç°¢ÇüÀ» ¿ø, Åõ, ¾²¸®, Æ÷ ±×¸³´Ï´Ù.

»ç°¢ÇüÀ» Áö¿ó´Ï´Ù

¾È³çÇϼ¼¿ä »ç°¢ÇüÀÔ´Ï´Ù

 

¿øÀ» ±×¸³´Ï´Ù

¿øÀ» Áö¿ó´Ï´Ù

 

»ï°¢ÇüÀ» Çϳª, µÑ, ¼Â, ±×¸³´Ï´Ù.

»ï°¢ÇüÀ» Áö¿ó´Ï´Ù

 

»ç°¢ÇüÀ» ¿ø, Åõ, ¾²¸®, Æ÷ ±×¸³´Ï´Ù.

»ç°¢ÇüÀ» Áö¿ó´Ï´Ù

 

´ç¿¬È÷ ´ÙÀ½°ú °°Àº °´Ã¼»ý¼ºÀÌ °¡´ÉÇÏ´Ù°í »ý°¢ÇÕ´Ï´Ù.

 

n                 Circle c  = new Circle();

n                 Triangle t = new Triangle();

n                 Rectangle r = new Rectangle();

 

¾÷ij½ºÆÃ(Upcasting)À» ÀÌ¿ëÇÏ¸é ´ÙÀ½°ú °°Àº °´Ã¼ »ý¼ºµµ °¡´ÉÇÕ´Ï´Ù.

 

n                 Shape c1 = new Circle();

n                 Shape t1 = new Triangle();

n                 Shape r1 = new Rectangle();

 

±×·±µ¥ À§ÀÇ Äڵ忡¼­ ¾÷ij½ºÆÃ(Upcasting)À» ÀÌ¿ëÇÏ´Â ºÎºÐ¿¡¼­ ÀÌ»óÇÑ Á¡À» ¹ß°ßÇÒ ¼ö ÀÖ½À´Ï´Ù. Shape c1, t1, r1Àº ¸ðµÎ ShapeŬ·¡½ºÀÇ °´Ã¼º¯¼öÀÔ´Ï´Ù. ÇÏÁö¸¸ ÆÄ»ýŬ·¡½ºÀÇ ¸Þ¸ð¸®¸¦ »ý¼ºÇÏ¿© ÇÒ´çÇÏ¿´½À´Ï´Ù. ÀÌ ¶§ c1, t1, r1ÀÌ ÀçÁ¤ÀÇµÈ ¸Þ¼­µå¸¦ È£ÃâÇßÀ» ¶§ ´©±¸ÀÇ ¸Þ¼­µå¸¦ È£ÃâÇÏ´À³Ä´Â °ÍÀÔ´Ï´Ù. ºÐ¸í c1, t1, r1Àº ¸ðµÎ ShapeÇüÀÔ´Ï´Ù. À̰ÍÀÇ ±¸Á¶´Â ¾Æ·¡ÀÇ ±×¸²°ú °°½À´Ï´Ù.

 

±×¸² 5-5 Upcasting°ú ¸Þ¸ð¸® ±¸Á¶

 

ÀϹÝÀûÀÎ °üÁ¡¿¡¼­ º»´Ù¸é, c1 ÀÚüÀÇ Å©±â´Â ShapeÇüÀ̱⠶§¹®¿¡ ½ÇÁ¦·Î »ý¼ºµÈ ¸Þ¸ð¸®¿¡¼­ ÇÚµé ÇÒ ¼ö ÀÖ´Â ºÎºÐÀº ShapeÀÇ Draw() ¸Þ¼­µå¿Í Delete() ¸Þ¼­µåÀÔ´Ï´Ù. ¸¸ÀÏ virtual ·Î ÀçÁ¤ÀÇµÈ ¸Þ¼­µå°¡ Circle¿¡ Á¸ÀçÇÑ´Ù¸é CircleÀÇ Draw() ¸Þ¼­µå¿Í Delete() ¸Þ¼­µå¸¦ »ç¿ëÇÕ´Ï´Ù. ¾ø´Ù¸é ShapeÀÇ °ÍÀ» ÀÌ¿ëÇÏÁö¸¸, ÀÖ´Ù¸é ¾ÆµéÀÇ °ÍÀ» ÀÌ¿ëÇÕ´Ï´Ù. ÀÌ·¯ÇÑ ¿ø¸®¸¦ ´Ù½Ã ±×¸²À¸·Î ³ªÅ¸³» º¸µµ·Ï ÇϰڽÀ´Ï´Ù.

 

±×¸² 5-6 Upcasting°ú ¸Þ¼­µå È£Ãâ

 

À§ÀÇ ±×¸²Àº Shape c1 = new Circle(); À̶ó°í °´Ã¼¸¦ »ý¼ºÇÑ ÈÄ c1À» ÀÌ¿ëÇÏ¿© c1.Draw() ¸Þ¼­µå¸¦ È£Ãâ ÇßÀ» ¶§ÀÇ ¼öÇà ¼ø¼­ÀÔ´Ï´Ù. ¸ÕÀú c1Àº ÀÚ½ÅÀÇ Draw() ¸Þ¼­µå ÂÊÀ¸·Î ³Ñ¾î°©´Ï´Ù. ±×¸®°í ÀçÁ¤ÀÇµÈ ¸Þ¼­µå°¡ ÀÖ´ÂÁö ¾ø´ÂÁö È®ÀÎ ÇÕ´Ï´Ù. ±×¸®°í ¾ÆµéÀÌ ÀçÁ¤ÀÇµÈ ¸Þ¼­µå¸¦ °¡Áö°í ÀÖ´Ù¸é ¾ÆµéÀÇ Draw() ¸Þ¼­µå¸¦ È£ÃâÇÏ°Ô µÇ´Â °ÍÀÔ´Ï´Ù. ÀÌ·¯ÇÑ Ãø¸é ¶§¹®¿¡ ÄÄÆÄÀÏ Å¸ÀÓ¿¡ ÀÌ·¯ÇÑ ÀÛ¾÷ÀÌ ÀϾ´Â °ÍÀÌ ¾Æ´Ï¶ó ·±Å¸ÀÓ¿¡ À§¿Í °°Àº Çö»óÀÌ ³ªÅ¸³³´Ï´Ù.

 

À̰ÍÀ» ¿ì¸®´Â ¾÷ij½ºÆÃ(Upcasting)À̶ó ºÎ¸£´Â °ÍÀÔ´Ï´Ù. Á¼Àº ¹æÇâÀ¸·Î ij½ºÆÃ(Casting)µÈ´Ù°í ÇÏ¿© Narrow CastingÀ̶ó°í ºÎ¸£±âµµ ÇÕ´Ï´Ù.

 

ÀÌ ¾÷ij½ºÆÃ(Upcasting)ÀÇ ±¸Á¶¿¡¼­ À¯ÀÇÇØ¾ß ÇÒ °ÍÀÌ Çϳª ÀÖ½À´Ï´Ù. ¼Ò½º¿¡¼­ º»´Ù¸é ¿¡·¯°¡ ³­ ºÎºÐÀÌ ÀÖ½À´Ï´Ù. ¿¡·¯°¡ ³ª´Â ÀÌÀ¯´Â Shape Ŭ·¡½º¿¡ ÇØ´çÇÏ´Â ¸Þ¼­µå°¡ Á¸ÀçÇÏÁö ¾Ê±â ¶§¹®ÀÔ´Ï´Ù. ÀçÁ¤ÀǵǾú´Ù¸é ã¾Æ °¥ ¼ö ÀÖÁö¸¸, ÀçÁ¤ÀǵÇÁö ¾Ê¾Ò´Ù¸é ã¾Æ °¥ ¼ö ¾ø½À´Ï´Ù. ±× ÀÌÀ¯¸¦ ±×¸²À¸·Î º»´Ù¸é ´ÙÀ½°ú °°À» °ÍÀÔ´Ï´Ù.

 

±×¸² 5-7 Upcasting½Ã À¯ÀÇ»çÇ×

 

¾ÆµéÀÌ ¸Þ¼­µå¸¦ ÀçÁ¤ÀÇÇßÀ» ¶§¸¸ Shape c1Àº ¾ÆµéÀÇ °ÍÀ» ã¾Æ °¥ ¼ö ÀÖ½À´Ï´Ù. ÀÌ ¸»Àº Shape¿¡ ÀÖ¾î¾ß¸¸ ±× ¸Þ¼­µå¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù´Â ¸»ÀÔ´Ï´Ù. Áï, c1Àº ShapeÇüÀ̶ó´Â °ÍÀÔ´Ï´Ù. ¾ÆÁÖ ´Ü¼øÇÑ ¸»ÀÌÁö¸¸ ¾÷ij½ºÆÃ(Upcasting) ¶§¹®¿¡ Çü ±× Â÷Á¦¸¦ ¸Á°¢ÇÏ¸é ¾ÈµË´Ï´Ù.



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