다음을 통해 공유


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

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

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

  • abstractshape.cs: Shape 추상 Area 속성을 포함하는 클래스입니다.

  • 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
{
    public Shape(string s)
    {
        // calling the set accessor of the Id property.
        Id = s;
    }

    public string Id { get; set; }

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

    public override string ToString()
    {
        return $"{Id} Area = {Area:F2}";
    }
}
  • 속성의 한정자는 속성 선언 자체에 배치됩니다. 다음은 그 예입니다.

    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)
    {
        _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)
    {
        _radius = radius;
    }

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

public class Rectangle : Shape
{
    private int _width;
    private int _height;

    public Rectangle(int width, int height, string id)
        : base(id)
    {
        _width = width;
        _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")
        ];

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

참고하십시오