如何:定义抽象属性(C# 编程指南)

本文介绍如何在C#中定义和使用抽象属性。通过示例代码展示了一个抽象基类Shape及其派生类Square、Circle和Rectangle的实现,各派生类重写了基类中的抽象属性以计算各自的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何:定义抽象属性(C# 编程指南)

 

下面的示例演示如何定义抽象属性。抽象属性声明不提供属性访问器的实现,它只声明该类支持属性,而将访问器实现留给派生类。下面的示例演示如何实现从基类继承的抽象属性。

此示例由三个文件组成,其中每个文件都单独编译,产生的程序集由下一次编译引用:

  • abstractshape.cs:包含抽象 Area 属性的 Shape 类。

  • shapes.cs:Shape 类的子类。

  • shapetest.cs:测试程序,它显示某些 Shape 派生对象的面积。

若要编译该示例,请使用以下命令:

csc abstractshape.cs shapes.cs shapetest.cs

这样将生成可执行文件 shapetest.exe。

示例

该文件声明的 Shape 类包含 double 类型的 Area 属性。

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

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

    public string Id
    {
        get
        {
            return m_id;
        }

        set
        {
            m_id = 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 m_side;

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

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

public class Circle : Shape
{
    private int m_radius;

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

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

public class Rectangle : Shape
{
    private int m_width;
    private int m_height;

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

    public override double Area
    {
        get
        {
            // Given the width and height, return the area of a rectangle:
            return m_width * m_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);
        }
    }
}
输出
Shapes Collection
Square #1 Area = 25.00
Circle #1 Area = 28.27
Rectangle #1 Area = 20.00
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值