Share via


方法 : 抽象プロパティを定義する (C# プログラミング ガイド)

更新 : 2007 年 11 月

次の例では、抽象プロパティの定義方法を示します。抽象プロパティの宣言では、プロパティ アクセサは実装されません。クラスがプロパティをサポートしていることは宣言しますが、アクセサの実装は派生クラスに委ねます。基本クラスから継承された抽象プロパティを実装する方法を次の例に示します。

この例は、次の 3 つのファイルで構成されています。各ファイルは個別にコンパイルされ、生成されたアセンブリが次のコンパイルによって参照されます。

  • 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 の 3 種類のサブクラスと、それらがどのように 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# プログラミング ガイド)