0
一個朋友最近進行了電話採訪,他被問了一個技術問題:2D的計算面積形狀
Q)如果我想計算一些2D圖形的表面積那麼什麼是「鬥」會我用。他有20分鐘的時間寫一些代碼,面試官給他回電話。他通過電子郵件發送了代碼,在面試的其餘部分沒有討論代碼(沒有其他技術問題)。他送我的代碼:
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);
}
}
}
}
形狀 - 類庫
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;
}
}
}
面試官說,他 「掙扎」 與測試。代碼有什麼問題?
爲什麼使用Windows形式如果結果將在控制檯被打印? – Fabio
pi是一個常量,爲什麼它作爲一個參數傳遞並且不正確地平方? – jdweng
所以我可以指定'pi = -15.789'? '... CalculateSurfaceArea(double pi,double radius)...';我可以用'長度'!='寬度'來設置*正方形? –