c# wpf 试题_求问一道 C#笔试题,想知道最好的解答是什么

这是一道关于C#笔试题的讨论,涉及重构计算几何形状(圆形、矩形、三角形)面积的代码。原始代码使用Shape类和ShapeCalculator类,存在冗余检查和条件判断。提出的问题是:如何改进这段代码以更优雅地计算多种形状的面积?回答者提出了使用接口ICalc和具体形状类(Circle、Rectangle、Triangle)的实现,通过LINQ简化了计算总区域的逻辑。寻求是否有更好的解决方案。
摘要由CSDN通过智能技术生成

题目如下:

这是一段用于计算多个几何形状面积之和的程序

指出程序段存在的不妥之处,并重构代码

在 1 的基础上增加一种可计算的几何形状类型三角形,其面积为 底×高×1/2

public class Shape

{

public int Type { get; set; }

public int Radius { get; set; }

public int Width { get; set; }

public int Length { get; set; }

}

public class ShapeCalculator

{

public static double CalculateTotalArea(List shapes)

{

double totalArea = 0;

if (shapes != null && shapes.Count() > 0)

{

foreach (Shape shape in shapes)

{

if (shape.Type == 1)

{

totalArea += Math.PI * shape.Radius * shape.Radius;

}

else if (shape.Type == 2)

{

totalArea += shape.Length * shape.Width;

}

}

}

return totalArea;

}

}

我的回答是这样的:

interface ICalc

{

double GetAera();

}

class Circle : ICalc

{

public int Radius { get; set; }

public double GetAera()

{

return Math.PI * Radius * Radius;

}

}

class Rectangle : ICalc

{

public int Length { get; set; }

public int Width { get; set; }

public double GetAera()

{

return Length * Width;

}

}

class Triangle : ICalc

{

public int Bottom { get; set; }

public int Height { get; set; }

public double GetAera()

{

return Bottom * Height * 0.5;

}

}

class ShapeCalculator

{

public static double CalculateTotalArea(List shapes)

{

double totalArea = 0;

if (shapes != null)

{

totalArea = shapes.Sum(shape => shape.GetAera());

}

return totalArea;

}

}

凭我的水平和理解能力就只有写到这种程度了……有什么更好的写法吗?

Microsoft's Windows Presentation Foundation (WPF) provides you with a development framework for building high-quality user experiences for the Windows operating system. It blends together rich content from a wide range of sources and allows you unparalleled access to the processing power of your Windows computer. Pro WPF 4.5 in C# provides a thorough, authoritative guide to how WPF really works. Packed with no-nonsense examples and practical advice you'll learn everything you need to know in order to use WPF in a professional setting. The book begins by building a firm foundation of elementary concepts, using your existing C# skills as a frame of reference, before moving on to discuss advanced concepts and demonstrate them in a hands-on way that emphasizes the time and effort savings that can be gained. What you’ll learn •Understand the fundamentals of WPF programming from XAML to controls and data flow. •Develop realistic application scenarios to see navigation, localization and deployment in action. •Explore the advanced user interface controls that WPF provides. •Learn to manage documents from within WPF: Text layout, printing, and document packaging are all covered. •Use graphics and multimedia to add punch to your applications Who this book is for This book is designed for developers encountering WPF for the first time in their professional lives. A working knowledge of C# and the basic architecture of .NET is helpful to follow the examples easily, but all concepts will be explained from the ground up. Table of Contents 01.Introducing WPF 02.XAML 03.Layout 04.Dependency Properties 05.Routed Events 06.Controls 07.The Application 08.Element Binding 09.Commands 10.Resources 11.Styles and Behaviors 12.Shapes, Brushes, and Transforms 13.Geometries and Drawings 14.Effects and Visuals 15.Animation Basics 16.Advanced Animation 17.Control Templates 18.Custom Elements 19.Data Binding 20.Formatting 21.Bound Data 22.Data Views 23.Lists, Trees, and Grids 24.Windows Pages and Navigation 25.Menus, Toolbars, and Ribbons 26. Sound and Video 27.3-D Drawing 28.Documents 29. Printing 30.Interacting with Windows Forms 31.Multithreading 32.The Add-in Model 33.ClickOnce Deployment ----------------------------------------------------------- Pro WPF 4th edition,喜欢的朋友请支持正版。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值