小编use*_*443的帖子

如何在子类中使用基类重写ToString()方法?

基类:

namespace inheritance
{
    public abstract class Shape
    {
        private string shapeId;
        private float length;

        public Shape(string s)
        {
            shapeId = s;
        }

        public string Id
        {
            get
            {
                return shapeId;
            }
            set
            {
                shapeId = value;
            }
        }

        public abstract double Area
        { get; 
        }

        public override string ToString()
        {
            return Id + "Area = " + string.Format("{0:F2}", Area);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我已经覆盖了该ToString方法.

这是我的子类:

namespace inheritance
{
    public class Circle : Shape
    {
        private double area;

        public …
Run Code Online (Sandbox Code Playgroud)

c#

0
推荐指数
1
解决办法
1778
查看次数

标签 统计

c# ×1