public abstract class Shape
{
private string name;
public Shape(string s)
{
Id = s;
}
public string Id
{
get
{
return name;
}
set
{
name = value;
}
}
public abstract double Area
{
get;
}
public override string ToString()
{
return Id + " Area = " + string.Format("{0:F2}", Area);
}
}
在此示例中,只有一个 get 访问器可用,因此该属性是只读的
属性来提供自己的实现。
public class Square : Shape
{
private int side;
public Square(int side, string id)
: base(id)
{
this.side = side;
}
public override double Area
{
get
{
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
{
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
{
return width * height;
}
}
}
派生对象,并输出它们的面积。
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# 定义抽象属性
最新推荐文章于 2023-07-18 14:52:15 发布