Define Abstract Properties


Contoh berikut menunjukkan bagaimana mendefinisikan properti abstrak. Deklarasi properti abstrak tidak menyediakan implementasi akses properti - ia menyatakan bahwa class mendukung properti, namun membiarkan implementasi accessor ke class turunan.

Contoh berikut menunjukkan bagaimana menerapkan properti abstrak yang inheritance dari base class. Sampel ini terdiri dari tiga file, masing-masing dikompilasi secara terpisah dan assembly yang dihasilkan reference oleh kompilasi berikutnya: abstractshape.cs: class Shape yang berisi properti Area abstrak.

shapes.cs: Subclass dari class Shape. shapetest.cs: Sebuah program uji untuk menampilkan shape beberapa objek yang diturunkan dari Shape. Untuk mengkompilasi contoh, gunakan perintah berikut:
csc abstractshape.cs shapes.cs shapetest.cs
Ini akan membuat file shapetest.exe yang bisa dieksekusi.
Contoh
File ini mendeklarasikan kelas Shape yang berisi properti Area dari tipe data double
// 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);
    }
}
Modifier pada properti ditempatkan pada deklarasi properti itu sendiri. Sebagai contoh:
public abstract double Area  
Saat mendeklarasikan properti abstrak (seperti Area pada contoh ini), kita cukup menunjukkan accessors properti mana yang tersedia, namun tidak menerapkannya. Dalam contoh ini, hanya mendapatkan accessor yang tersedia, jadi properti itu hanya bisa dibaca.

  • Contoh


Code berikut menunjukkan tiga subclass Shape dan bagaimana mereka mengganti properti Area untuk memberikan implementasinya sendiri.
// 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;
        }
    }
}

  • Contoh

Code berikut menunjukkan program uji yang membuat sejumlah objek yang diturunkan dari Shape dan mencetak area mereka.
// 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
*/
Previous
Next Post »