Share via


방법: 추상 속성 정의(C# 프로그래밍 가이드)

업데이트: 2007년 11월

다음 예제에서는 추상 속성을 정의하는 방법을 보여 줍니다. 추상 속성 선언에서는 속성 접근자에 대한 구현을 제공하지 않습니다. 여기서는 클래스가 속성을 지원하도록 선언하지만 접근자 구현은 파생 클래스의 몫으로 남겨 둡니다. 다음 예제에서는 기본 클래스에서 상속된 추상 속성을 구현하는 방법을 보여 줍니다.

이 샘플은 파일 세 개로 구성되어 있습니다. 각 파일은 개별적으로 컴파일되고 그 결과 어셈블리는 다음 컴파일에서 참조합니다.

  • abstractshape.cs: 추상 Area 속성이 들어 있는 Shape 클래스입니다.

  • shapes.cs: Shape 클래스의 서브클래스입니다.

  • shapetest.cs: Shape 파생 개체 일부의 영역을 표시하기 위한 테스트 프로그램입니다.

예제를 컴파일하려면 다음 명령을 사용해야 합니다.

csc abstractshape.cs shapes.cs shapetest.cs

이렇게 하면 실행 파일 shapetest.exe가 만들어집니다.

예제

이 파일에서는 double 형식의 Area 속성을 포함하는 Shape 클래스를 선언합니다.

// compile with: csc /target:library abstractshape.cs
public abstract class Shape
{
    private string name;

    public Shape(string s)
    {
        // calling the set accessor of the Id property.
        Id = s;
    }

    public string Id
    {
        get
        {
            return name;
        }

        set
        {
            name = value;
        }
    }

    // Area is a read-only property - only a get accessor is needed:
    public abstract double Area
    {
        get;
    }

    public override string ToString()
    {
        return Id + " Area = " + string.Format("{0:F2}", Area);
    }
}
  • 속성에 대한 한정자는 속성 선언 자체에 배치됩니다. 예를 들면 다음과 같습니다.

    public abstract double Area
    
  • 이 예제에서의 Area와 같이 추상 속성을 선언할 때 단순히 사용할 수 있는 속성 접근자를 나타내고 구현하지는 않습니다. 이 예제에서는 get 접근자만 사용할 수 있으므로 속성이 읽기 전용입니다.

다음 코드에서는 Shape의 서브클래스 세 개를 보여 주고 이 서브클래스에서 Area 속성을 재정의하여 자체 구현을 제공하는 방법을 보여 줍니다.

// compile with: csc /target:library /reference:abstractshape.dll shapes.cs
public class Square : Shape
{
    private int side;

    public Square(int side, string id)
        : base(id)
    {
        this.side = side;
    }

    public override double Area
    {
        get
        {
            // Given the side, return the area of a square:
            return side * side;
        }
    }
}

public class Circle : Shape
{
    private int radius;

    public Circle(int radius, string id)
        : base(id)
    {
        this.radius = radius;
    }

    public override double Area
    {
        get
        {
            // Given the radius, return the area of a circle:
            return radius * radius * System.Math.PI;
        }
    }
}

public class Rectangle : Shape
{
    private int width;
    private int height;

    public Rectangle(int width, int height, string id)
        : base(id)
    {
        this.width = width;
        this.height = height;
    }

    public override double Area
    {
        get
        {
            // Given the width and height, return the area of a rectangle:
            return width * height;
        }
    }
}

다음은 여러 Shape 파생 개체를 만들고 해당 면적을 출력하는 테스트 프로그램 코드입니다.

// compile with: csc /reference:abstractshape.dll;shapes.dll shapetest.cs
class TestClass
{
    static void Main()
    {
        Shape[] shapes =
        {
            new Square(5, "Square #1"),
            new Circle(3, "Circle #1"),
            new Rectangle( 4, 5, "Rectangle #1")
        };

        System.Console.WriteLine("Shapes Collection");
        foreach (Shape s in shapes)
        {
            System.Console.WriteLine(s);
        }
    }
}
/* Output:
    Shapes Collection
    Square #1 Area = 25.00
    Circle #1 Area = 28.27
    Rectangle #1 Area = 20.00
*/

참고 항목

작업

방법: C# DLL 만들기 및 사용(C# 프로그래밍 가이드)

개념

C# 프로그래밍 가이드

참조

클래스 및 구조체(C# 프로그래밍 가이드)

추상 및 봉인 클래스와 클래스 멤버(C# 프로그래밍 가이드)

속성(C# 프로그래밍 가이드)