Bagikan melalui


Cara menentukan properti abstrak (Panduan Pemrograman C#)

Contoh berikut menunjukkan cara menentukan properti abstrak . Deklarasi properti abstrak tidak menyediakan implementasi pengakses properti -- ia menyatakan bahwa kelas mendukung properti, tetapi meninggalkan implementasi aksesor ke kelas turunan. Contoh berikut menunjukkan cara mengimplementasikan properti abstrak yang diwarisi dari kelas dasar.

Sampel ini terdiri dari tiga file, yang masing-masing dikompilasi secara individual dan rakitan yang dihasilkan dirujuk oleh kompilasi berikutnya:

  • abstractshape.cs: kelas Shape yang memiliki properti abstrak Area.

  • shapes.cs: Subkelas dari kelas Shape.

  • shapetest.cs: Sebuah program pengujian untuk menampilkan area dari beberapa objek turunan Shape.

Untuk mengkompilasi contoh, gunakan perintah berikut:

csc abstractshape.cs shapes.cs shapetest.cs

Ini akan membuat file yang dapat dieksekusi shapetest.exe.

Contoh

File ini mendeklarasikan kelas Shape yang berisi atribut Area dari jenis double.

// 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}";
    }
}
  • Pengubah pada properti ditempatkan pada deklarasi properti itu sendiri. Contohnya:

    public abstract double Area  
    
  • Saat mendeklarasikan properti abstrak (seperti Area dalam contoh ini), Anda cukup menunjukkan aksesor properti apa yang tersedia, tetapi tidak mengimplementasikannya. Dalam contoh ini, hanya aksesor get yang tersedia, sehingga properti bersifat baca-saja.

Kode berikut menunjukkan tiga subkelas dari Shape dan bagaimana mereka meng-override properti Area untuk memberikan implementasi mereka 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)
    {
        _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;
        }
    }
}

Kode berikut menunjukkan sebuah program uji yang membuat sejumlah objek-turunan Shape dan mencetak areanya.

// 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
*/

Lihat juga