¼Ò¼³°°ÀºC#  Á¦4°­ÀÇ C#ÀÇ °´Ã¼ÁöÇâ  04. ´Ü°èº°»ó¼Ó  

 

 

4.4 ´Ü°èº° »ó¼Ó(Inheritance)

 

ÀÌ Àý¿¡¼­´Â 1 ·¹º§ »ó¼ÓÇÏ´Â ¹æ¹ý°ú 2 ·¹º§¿¡ °ÉÃÄ »ó¼ÓÇÏ´Â ¿¹Á¦¸¦ ´Ù·ç¾î º¸µµ·Ï ÇϰڽÀ´Ï´Ù. ÀÏ´Ü ÇҾƹöÁöŬ·¡½º¸¦ ¸¸µéÁÒ. ÇҾƹöÁöŬ·¡½º¸¦ »ó¼Ó ¹Þ¾Æ ¾Æ¹öÁöŬ·¡½º¸¦ ¸¸µé°í ¾Æ¹öÁöŬ·¡½º¸¦ ÀÌ¿ëÇÏ¿© °´Ã¼¸¦ ¸¸µé°Ú½À´Ï´Ù. ±×¸®°í ¾Æ¹öÁö °´Ã¼¿¡¼­ ÇҾƹöÁö ¼Ò¼ÓÀÇ ¸Þ¼­µåµé È£ÃâÇØ º¸µµ·Ï ÇϰڽÀ´Ï´Ù. ÀÌ·¸°Ô µÈ´Ù¸é ¾Æ¹öÁö°¡ ÇҾƹöÁö °ÍÀ» ÀÌ¿ëÇÏ´Â °ÍÀÌ µÇ°ÚÁÒ. Áö±Ý ¼³¸íÇÑ ¿¹Á¦´Â ¾Æ·¡¿Í °°½À´Ï´Ù.

 

&

InheritanceTest1.cs

Ü »ó¼Ó °³³äÀ» Å×½ºÆ®Çϱâ À§ÇÑ ¿¹Á¦

using System;

public class GrandFather {

           public GrandFather() {

                    Console.WriteLine(" I am a GrandFather");

           }

           public void SayGrandNumber() {

                    for(int i=0; i<3; i++) {

                          Console.Write( i + "\t");

                    }

           }                        

}

public class Father: GrandFather {

           public Father() {

                    Console.WriteLine(" I am a Father");

           }

           public void SayFatherNumber() {

                    for(int i=0; i<6; i++){

                          Console.Write( i + "\t");

                    }

           }                        

}

public class InheritanceTest1 {

           public static void Main() {

                    Father f = new Father();

                          Console.WriteLine("GrandFatherÀÇ sayGrandNumber");

                           f.SayGrandNumber(); // ÇҾƹöÁöÀÇ ¸Þ¼­µå È£Ãâ

                    Console.WriteLine("");

                          Console.WriteLine("FatherÀÇ sayFatherNumber");

                           f.SayFatherNumber(); // ¾Æ¹öÁö ÀÚ½ÅÀÇ ¸Þ¼­µå È£Ãâ

           }

}

C:\C#Example\04>csc InheritanceTest1.cs

C:\C#Example\04>InheritanceTest1

 I am a GrandFather

 I am a Father

GrandFatherÀÇ sayGrandNumber

0       1       2

FatherÀÇ sayFatherNumber

0       1       2       3       4       5

 

À§ÀÇ ¿¹Á¦¿¡¼­ Father Ŭ·¡½º´Â GrandFather Ŭ·¡½º¸¦ »ó¼ÓÇϰí ÀÖ½À´Ï´Ù. GrandFather Ŭ·¡½º¸¦ »ó¼Ó ¹Þ´Â ¼ø°£ Father Ŭ·¡½º´Â GrandFather Ŭ·¡½ºÀÇ ¸ðµç °ÍÀ» °¡Áö°í ½ÃÀÛÇÑ´Ù°í º¸¸é µË´Ï´Ù. Father Ŭ·¡½º¸¦ Å×½ºÆ®Çϱâ À§Çؼ­ InheritanceTest Ŭ·¡½ºÀÇ main() ¸Þ¼­µå¿¡¼­ Father Ŭ·¡½º °´Ã¼ f¸¦ »ý¼ºÇϰí, Father Ŭ·¡½º °´Ã¼´Â GrandFather Ŭ·¡½ºÀÇ °ÍÀ» ÀÚ½ÅÀÇ °Íó·³ È£Ãâ Çϰí ÀÖ½À´Ï´Ù. À̰ÍÀÌ ¹Ù·Î »ó¼ÓÀÇ ÀåÁ¡ÀÔ´Ï´Ù. »ý¼ºÀÚ¸¦ Á¦¿ÜÇÑ´Ù¸é Father Ŭ·¡½º´Â ¾Æ¹öÁö Ŭ·¡½ºÀÇ ¸Þ¼­µå¿Í ÀÚ½ÅÀÇ ¸Þ¼­µå 2°³¸¦ °¡Áö°Ô µË´Ï´Ù. ´ÙÀ½Àº À§ÀÇ ÇÁ·Î±×·¥ÀÇ ±¸Á¶¸¦ º¸¿© ÁÖ°í ÀÖ½À´Ï´Ù.

 

 

 

1 ·¹º§ »ó¼ÓÀ» ÇÑ ÇÁ·Î±×·¥ÀÇ ±¸Á¶

 

¾Æ¹öÁö°¡ ÇҾƹöÁö¸¦ »ó¼Ó ¹Þ¾Ò±â ¶§¹®¿¡ ÇҾƹöÁöÀÇ ¸Þ¼­µå¸¦ ¾Æ¹öÁö°¡ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. ÇÏÁö¸¸ °á°ú¿¡¼­ ÀÌ»óÇÑ ºÎºÐÀ» ¹ß°ßÇÒ ¼ö ÀÖ½À´Ï´Ù. ¹Ù·Î »ý¼ºÀÚÀÇ È£ÃâÀÔ´Ï´Ù. ¾Æ¹öÁö °´Ã¼¸¦ ¸¸µå´Âµ¥ ÇҾƹöÁöÀÇ »ý¼ºÀÚµµ È£ÃâµÇ°í ÀÖ½À´Ï´Ù. ÀÌ»óÇÑ ÀÏÀÌÁÒ. À̰ÍÀº ÀÌ·¸°Ô ¼³¸íÇÒ ¼ö ÀÖ½À´Ï´Ù. »ó¼Ó°úÁ¤¿¡¼­ »óÀ§·¹º§ Ŭ·¡½ºÀÇ ¸Þ¸ð¸®°¡ »ý¼ºµÇÁö ¾Ê´Â´Ù¸é Àڽķ¹º§ÀÇ ¸Þ¸ð¸®´Â »ý¼ºÇÒ ¼ö ¾ø½À´Ï´Ù. ±×·¸±â ¶§¹®¿¡ Father Ŭ·¡½º·Î °´Ã¼¸¦ ¸¸µé¾ú´Ù¸é Father Ŭ·¡½º°¡ »ó¼Ó ¹ÞÀº ¸ðµç »óÀ§·¹º§ÀÇ »ý¼ºÀÚ°¡ Â÷·Ê´ë·Î È£Ã⠵ǾîÁö°í, Á¦ÀÏ ¸¶Áö¸·¿¡ ÀÚ½ÅÀÇ °ÍÀÌ È£ÃâµÇ¾îÁý´Ï´Ù. ÇҾƹöÁö°¡ ¾ø´Ù¸é ¾Æ¹öÁöµµ ¾ø°ÚÁÒ. ±×·¡¼­ ÇҾƹöÁö »ý¼ºÀÚ°¡ È£Ãâ µÈ ÈÄ. ¾Æ¹öÁöÀÇ »ý¼ºÀÚ°¡ È£ÃâµË´Ï´Ù. ÇҾƹöÁö, ¾Æ¹öÁö, ¾Æµé±îÁö 3´Ü°è·Î ³ª´©¾î¼­ »ó¼ÓÇØµµ ÀÌ ±ÔÄ¢À» ¾î±âÁö´Â ¾Ê½À´Ï´Ù. ¾Æ·¡´Â 3´Ü°èÀÇ ±¸Á¶¿Í ÇÁ·Î±×·¥ÀÇ ¿¹ÀÔ´Ï´Ù.

 

 

 

±×¸² 4-1 ·¹º§ »ó¼ÓÀ» ÇÑ ÇÁ·Î±×·¥ÀÇ ±¸Á¶

 

&

InheritanceTest2.cs

Ü 3´Ü°è »ó¼ÓÀ» À§ÇÑ ¿¹Á¦

using System;

public class GrandFather {

        public GrandFather() {

                  Console.WriteLine(" I am a GrandFather");

        }

         void SayGrandNumber() {

                  for(int i=0; i<3; i++) {

                          Console.Write( i + "\t");

                  }

        }                        

}

public class Father: GrandFather {

        public Father() {

                  Console.WriteLine(" I am a Father");

        }

        public void SayFatherNumber() {

                  for(int i=0; i<6; i++){

                          Console.Write( i + "\t");

                  }

        }                        

}

public class Child: Father {

        public Child(){

                  Console.WriteLine(" I am a Child");

        }

        public void SayChildNumber() {

                  for(int i =0; i<9; i++){

                          Console.Write( i + "\t");

                  }

        }                        

}

public class InheritanceTest2 {

        public static void Main() {

                  Child c = new Child();

                        Console.WriteLine("GrandFatherÀÇ sayGrandNumber");

                  c.SayGrandNumber();

                  Console.WriteLine();

                        Console.WriteLine("FatherÀÇ sayFatherNumber");

                  c.SayFatherNumber();

                  Console.WriteLine();

                  Console.WriteLine("Child sayChildNumber");

                  c.SayChildNumber();

        }

}

 

C:\C#Example\04>csc InheritanceTest2.cs

C:\C#Example\04>InheritanceTest2

 I am a GrandFather

 I am a Father

 I am a Child

GrandFatherÀÇ sayGrandNumber

0     1     2

FatherÀÇ sayFatherNumber

0     1     2     3     4     5

Child sayChildNumber

0     1     2     3     4     5     6     7     8

 

»ó¼Ó´Ü°è°¡ ´Ã¾î³ª´õ¶óµµ ±âº»ÀûÀÎ ¹ýÄ¢Àº ¶È°°½À´Ï´Ù. ¸Þ¸ð¸®´Â ÃÖ»óÀ§Å¬·¡½ººÎÅÍ Â÷·Ê´ë·Î »ý¼ºµÇ¸ç Child Ŭ·¡½º´Â ÃÖÇÏÀ§ Ŭ·¡½ºÀε¥µµ ±× »óÀ§ÀÇ Å¬·¡½º¿¡ ¼ÓÇØÀÖ´Â ¸Þ¼­µå¸¦ ¸ðµÎ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.

 

¢Ã »ó¼Ó(Inheritance)ÀÇ Æ¯Â¡µé

q                »ý¼ºÀÚ´Â »óÀ§Å¬·¡½º·ÎºÎÅÍ »ó¼ÓµÇÁö¾Ê°í È£ÃâµÈ´Ù.

q                Ŭ·¡½º´Â Áߺ¹»ó¼ÓÀ» ÇÒ ¼ö ¾ø°í ´ÜÀÏ »ó¼Ó¸¸ÀÌ °¡´ÉÇÏ´Ù.

q                ÀÎÅÍÆäÀ̽º´Â Áߺ¹»ó¼ÓÀÌ °¡´ÉÇÏ´Ù.



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