使用poymorphism和虚方法计算区域 - 没有得到正确的结果。

时间:2016-11-27 14:23:58

标签: c# .net visual-studio

我编写了一个代码来使用多态(Virtua& Override Method)计算不同形状的区域,但没有得到正确的结果。 :(

以下是我的代码:

class Program
{
    static void Main(string[] args)
    {
        double side = 0;
        double length = 0;
        double width = 0;
        double height = 0;
        double baseoftriangle = 0;
        double radius = 0;

        UserChoice:
        Console.WriteLine("For what shape you want to calculate the Area:\n1. Sqaure\n2. Rectangle\n3. Triangle\n4. Circle");
        Console.Write("Please Select the number from above options: ");
        int choice = int.Parse(Console.ReadLine());



        switch (choice)
        {
            case 1:
                Console.Write("Please enter the side of square: ");
                side = double.Parse(Console.ReadLine());
                break;
            case 2:
                Console.Write("Please enter the length of rectangle: ");
                length = double.Parse(Console.ReadLine());
                Console.Write("Please enter the width of rectangle: ");
                width = double.Parse(Console.ReadLine());
                break;
            case 3:
                Console.Write("Please enter the height of triangle: ");
                height = double.Parse(Console.ReadLine());
                Console.Write("Please enter the base of triangle: ");
                baseoftriangle = double.Parse(Console.ReadLine());
                break;
            case 4:
                Console.Write("Please enter the radius of circle: ");
                radius = double.Parse(Console.ReadLine());
                break;
            default:
                Console.WriteLine("Incorrect Choice, please try again!");
                goto UserChoice;
        }

        CalculateArea Sqa = new Square();
        Sqa = new Rectangle();
        Sqa = new Triangle();
        Sqa = new Circle();
        if (choice == 1)
        {
            Sqa.Area(side);
            Sqa.ShowResult();
        }
        else if(choice==2)
        {
            Sqa.Area(length,width);
            Sqa.ShowResult();
        }
        else if(choice==3)
        {
            Sqa.Area(height, baseoftriangle);
            Sqa.ShowResult();
        }
        else
        {
            Sqa.Area(radius);
            Sqa.ShowResult();
        }


        ChoiceOfAnotherCalculation:
        Console.Write("\nDo you want to calculate area of any other shape? Give input in Yes or NO: ");
        string choice1 = Console.ReadLine();

        switch (choice1.ToUpper())
        {
            case "YES":
                goto UserChoice;
            case "NO":
                break;
            default:
                Console.WriteLine("Incorrect Choice, please try again!");
                goto ChoiceOfAnotherCalculation;
        }
    }
}
class CalculateArea
{
    public double result;
    public virtual void Area(double side)
    {
    }
    public virtual void Area(double length, double width)
    {
    }
    public void ShowResult()
    {
        Console.WriteLine($"Your Result is {result}");
    }
}
class Square: CalculateArea
{

    public override void Area(double side)
    {
        result = side * side;
    }

}
class Rectangle:CalculateArea
{
    public override void Area(double length, double width)
    {
        result = length * width;
    }
}
class Triangle:CalculateArea
{
    public override void Area(double height, double baseoftriangle)
    {
        result = (height * baseoftriangle)/2;
    }
}
class Circle:CalculateArea
{
    public override void Area(double radius)
    {
        result = 3.14159 * radius * radius;
    }
}

对于矩形&三角形,我总是得到0因为正方形我得到圆圈的结果。我认为这个问题是由于我为类创建对象的方式造成的(可能是因为多态性缺乏理解)。

有人可以看看,让我知道这里的实际问题是什么。

除此之外,如果您能为我提供解决方案,我将非常感谢您的努力。设置每个类的属性。我试图编写代码(我试图在每个派生类和基类中定义属性)但是我遇到了各种错误,这就是为什么我跳过上面的解决方案。

我也会请求请不要低估这个问题因为系统不允许我长时间提问。因为这是我能够正确理解事物的唯一平台。

非常感谢。

2 个答案:

答案 0 :(得分:0)

switch语句中,在案例1之后,您的程序将退出。这是因为您使用了return;,而您可能打算使用break;。 Break语句确保您的开关不会落入下一个语句,并且return语句将立即终止执行。

正确的案例陈述应该是这样的

case 1:
    Console.Write("Please enter the side of square: ");
    side = int.Parse(Console.ReadLine());
    break;

修改

基类只有一个抽象方法来计算区域,这应该通过继承类来实现。

public abstract class Shape
{
    // must implement by inheriting classes
    public abstract double CalculateArea();
}

每个形状都会使用不同的参数计算面积,因此您应该创建特定形状所需的属性。

public class Square : Shape
{
    // properties differ based on the shape.. 
    // eg: triangle needs height and base
    // edit CalculateArea method based on the shape
    public double Length { get; set; }

    public override double CalculateArea()
    {
        return Length * Length;
    }
}

像以前一样实现,创建形状的实例然后传递所需的参数并调用CalculateArea()方法,这将返回double值:

double length;
Console.WriteLine("Enter length of one side: ");
if (!double.TryParse(Console.ReadLine(), out length))
    Console.WriteLine("Invalid value, must be a number or decimal value.");

Square sq = new Square();
sq.Length = length;
Console.WriteLine(sq.CalculateArea().ToString("F"));

答案 1 :(得分:0)

对于多态,您的类应如下所示:

abstract class Shape
{
    public abstract double Area {get;}
}

class Square: Shape
{
    public double Side {get;}
    public override double Area => Side * Side;
    public Square (double side)
    {
       this.Side = side;
    }
}

class Rectangle:Shape
{
    public double Width {get;}
    public double Length {get;}
    public override double Area => Width * Length;
    public Rectangle (double width, double length)
    {   
       this.Width = width;
       this.Length = length;
    }
}

有了这个,您可以在第一个case语句中创建一次Shape,然后可以在输出中使用.Area属性。

你不应该在你的形状类中使用代码进行输出,让每个类都集中在一件事(SRP)上。

您的形状应该跟踪它们自己的属性,因为您稍后可能想要添加一个“描述”属性,该属性使用这些属性来描述形状。