본문 바로가기
C#(CSharp)/기초강좌(Basic)

[17] C# 상속 (C# Inheritance)

by swconsulting 2015. 2. 23.

1) 기본 개념

클래스에 다른 중요한 개념인 상속에 대해서 설명하겠습니다.


우선 간단한 코드를 보겠습니다.

public class A
{
    public A() { }
}

public class B : A
{
    public B() { }
}


B클래스 A를 상속 했습니다. 일반적으로 A를 부모(Parent) B를 자식(Child)으로 설명합니다. B는 자식이기 때문에 A에 있는 public, protected로 선언 되어 있는 멤버 변수, 멤버 함수 접근이 가능합니다.


좀더 구체적인 예제를 보겠습니다.


using System;

public class ParentClass
{
    public ParentClass()
    {
        Console.WriteLine("Parent Constructor.");
    }

    public
 void print()
    {
        Console.WriteLine("I'm a Parent Class.");
    }
}

public
 class ChildClass : ParentClass
{
    public ChildClass()
    {
        Console.WriteLine("Child Constructor.");
    }

    public
 static void Main()
    {
        ChildClass child = 
new ChildClass();

        child.print();
    }
}


[결과]

Parent Constructor.
Child Constructor.
I'm a Parent Class.


2)C# 상속 초기화 하기 (Base Class Initialization)


using System;
namespace RectangleApplication
{
   class Rectangle
   {
      //member variables
      protected double length;
      protected double width;
      public Rectangle(double l, double w)
      {
         length = l;
         width = w;
      }
      public double GetArea()
      {
         return length * width;
      }
      public void Display()
      {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }//end class Rectangle  
   class Tabletop : Rectangle
   {
      private double cost;
      public Tabletop(double l, double w) : base(l, w)
      { }
      public double GetCost()
      {
         double cost;
         cost = GetArea() * 70;
         return cost;
      }
      public void Display()
      {
         base.Display();
         Console.WriteLine("Cost: {0}", GetCost());
      }
   }
   class ExecuteRectangle
   {
      static void Main(string[] args)
      {
         Tabletop t = new Tabletop(4.5, 7.5);
         t.Display();
         Console.ReadLine();
      }
   }
}


Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5


상속을 받은 클래스에서 상위 클래스 생성자를 초기화 할때에는 아래 처럼 하면 됩니다.


      public Tabletop(double l, double w) : base(l, w)

      { }


3) 다중 상속 (Multiple Inheritance in C#)

C#은 다중 상속은 허용하지 않습니다. 하지만 다중 인터페이스 상속은 허용합니다.


using System;
namespace InheritanceApplication
{
   class Shape 
   {
      public void setWidth(int w)
      {
         width = w;
      }
      public void setHeight(int h)
      {
         height = h;
      }
      protected int width;
      protected int height;
   }

   // Base class PaintCost
   public interface PaintCost 
   {
      int getCost(int area);

   }
   // Derived class
   class Rectangle : Shape, PaintCost
   {
      public int getArea()
      {
         return (width * height);
      }
      public int getCost(int area)
      {
         return area * 70;
      }
   }
   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();
         int area;
         Rect.setWidth(5);
         Rect.setHeight(7);
         area = Rect.getArea();
         // Print the area of the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}
Total area: 35
Total paint cost: $2450


참고 : http://www.tutorialspoint.com/csharp/index.htm