繁体   English   中英

创建一个计算面积的程序

[英]Creating a program that calculates area

我遇到了四个错误。

在用于打印形状区域的WriteLine块中,变量“ area”出现的两个位置均给出错误消息:“名称“ area”在当前上下文中不存在”。 第二个问题是在Rectangle类中:'ComputeArea'的GeometricFigure,错误显示为''Rectangle.ComputeArea()'隐藏继承的成员'GeometricFigure.ComputeArea()'。如果打算隐藏,请使用new关键字。” 最后一个错误是在Triangle:GeometricFigure类中,并且涉及“ public Triangle(int x,int y)”表达式中的“ Triangle”。 错误消息显示为“'Rectangle.ComputeArea()'隐藏继承的成员'GeometricFigure.ComputeArea()'。如果打算隐藏,请使用new关键字。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace ShapesDemo
{
    class Program
{
    static void Main(string[] args)
    {
        Rectangle rec = new Rectangle(8, 10);
        Square squ = new Square(11, 12);
        Triangle tri = new Triangle(10, 20);
        Console.WriteLine("Computed area is {0}" + "\n\n" + "Computed Triangle is: {1}" + "\n",
            squ.ComputeArea(area), tri.ComputeArea(area));
    }
}
abstract class GeometricFigure
{
    public GeometricFigure(decimal sideA, decimal sideB)
    {
        this.height = sideA;
        this.width = sideB;
    }
    protected decimal area;
    protected decimal width;
    protected decimal height;
    public decimal Height
    {
        get
        {
            return height;
        }
        set
        {
            height = value;
            ComputeArea();
        } 
    }
    public decimal Width
    {
        get
        {
            return width;
        }
        set { width = value; }
    }
    public decimal Area
    {
        get { return area; }
        set { area = value; }
    }
    public void ComputeArea()
    {
    }
}
class Rectangle : GeometricFigure
{
    public Rectangle(decimal sideA, decimal sideB) : base(sideA, sideB)
    {

    }
    public void ComputeArea()
    {
        area = width * height;
        WriteLine("The Area is" + width.ToString(), height.ToString());
    }
    static void Display(Rectangle rec)
    {

    }
}
class Square : GeometricFigure
{
    static void Display(Square squ)
    {
    }
    public Square(decimal sideA, decimal sideB) : base(sideA, sideA)
    {
    }
}
class Triangle : GeometricFigure
{
    static void Display(Triangle tri)
    {
    }
    public Triangle(int x, int y)
    {
        this.Width = x;
        this.Height = y;
    }
}

}

名称区域不存在,所以您不能使用它。 Main()方法无权访问区域。 我认为您正在尝试做的是:

class Program
    {
        static void Main(string[] args)
        {
            Rectangle rec = new Rectangle(8, 10);
            Square squ = new Square(11, 12);
            squ.ComputeArea();
            Triangle tri = new Triangle(10, 20);
            tri.ComputeArea();
            Console.WriteLine("Computed area is {0}" + "\n\n" + "Computed Triangle is: {1}" + "\n",
            squ.Area, tri.Area);
            Console.ReadLine();
        }
    }

但是,您也面临更大的设计问题。 使用GeometricFigure基类将给您带来很多问题。 我会完全使用它或使用接口代替。 另外,您的Triangle必须是:

public Triangle(decimal sideA, decimal sideB) : base(sideA, sideA)
        {
            this.Width = sideA;
            this.Height = sideB;
        }

Microsoft文档提供了一个很好的示例,您可以尝试在这里完成什么,通常您希望:

  1. 抽象什么是常见的(在这种情况下,它计算面积)
  2. 指定混凝土中不常见的东西

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-define-abstract-properties

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM