简体   繁体   English

计算二维形状的表面积

[英]Calculate Surface Area of 2D shapes

A friend recently had a telephone interview and he was asked a technical question: 最近有一个朋友接受电话采访,有人问他一个技术问题:

Q) If I wanted to calculate the surface area of some 2D shapes then what "Bucket" would I use. 问:如果我想计算某些2D形状的表面积,那么我将使用什么“桶”。 He had 20 minutes to write some code and the interviewer called him back. 他有20分钟的时间写一些代码,面试官给他回了电话。 He sent the code via email and the code was not discussed for the remainder of the interview (there were no other technical questions). 他通过电子邮件发送了验证码,在剩余的采访中没有讨论验证码(没有其他技术问题)。 He sent me the code: 他给我发了代码:

Windows Forms app Windows窗体应用

namespace ShapesApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += form_load;
        }

        public void form_load (Object o, EventArgs e)
        {
            List<Shape> listShape = new List<Shape>();
            Shapes.Circle circle = new Shapes.Circle();
            Shapes.Rectangle rectangle = new Shapes.Rectangle();
            Shapes.Square square = new Shapes.Square();
            Shapes.Triangle triangle = new Shapes.Triangle();
            listShape.Add(rectangle);
            listShape.Add(square);
            listShape.Add(triangle);

            foreach (Shape shape in listShape)
            {
                double a = 10;
                double b = 10;
                double surfaceArea = shape.CalculateSurfaceArea(a,b);
                Console.WriteLine("The surface area of a " + shape.GetType() + " is: " + surfaceArea);
            }
        }

    }
}

Shapes - Class Library 形状-类库

namespace Shapes
{
    public abstract class Shape
    {
        abstract public double CalculateSurfaceArea(double Double1, double Double2);
    }

    public class Circle : Shape
    {
        public override double CalculateSurfaceArea(double pi, double radius)
        {
            return (pi * radius) * (pi * radius);
        }
    }

    public class Triangle : Shape
    {
        public override double CalculateSurfaceArea(double Base, double Height)
        {
            return (Base*Height)/2;
        }
    }

    public class Rectangle : Shape
    {
        public override double CalculateSurfaceArea(double Length, double Width)
        {
            return Length * Width;
        }
    }

}

The interviewer has said that he "struggled" with the test. 面试官说他在考试中“挣扎”。 What is wrong with the code? 代码有什么问题?

Calculating area is the behavior and every shape has his own formula for calculating it. 计算面积就是行为,每个形状都有自己的公式来计算。 Because calculating area can involve different amount of variables and constants method will not take any parameter and variables will be concern of class which implement interface . 因为计算区域可能涉及不同数量的变量,常量方法不会包含任何参数,变量将是实现接口的类的重点。

So I think method of calculating area can be abstracted as interface: 所以我认为计算面积的方法可以抽象为接口:

public interface ICalculatingArea
{
    double CalculateArea();
}

Then every shape will implement it on its own manner. 然后,每种形状都会以自己的方式实施。

public class Rectangle:ICalculatingArea
{

    public double Width {get; set;}
    public double Length {get; set;}

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

In the main program it is enough to cast shape classes to the interface type and use CalculateArea method 在主程序中,将形状类转换为接口类型并使用CalculateArea方法就足够了

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

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