创建一个C#库项目,用于计算集合中所有形状的总面积。 [英] Create a C# library project that calculates the total area of all shapes in a collection.

查看:102
本文介绍了创建一个C#库项目,用于计算集合中所有形状的总面积。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个C#库项目,用于计算集合中所有形状的总面积。

详情

为简单起见,下面需要两种形状

Circle

矩形

将来支持添加不同类型的形状,最小化代码更改。

每个形状对象包含以下两个属性。

名称

1.形状的名称,例如红色圆圈,蓝色矩形,由呼叫者或用户提供

2.集合中形状类型的标识。也就是说,形状的名称对于该集合中的那种形状是唯一的。



创建日期

1.日期当创建形状对象时,例如2019年6月12日

2.允许在工作日创建圈子,不允许在周末创建。

3.允许在周末创建矩形,而不是允许在工作日创建

最后,计算一个集合中所有形状的总面积。



我尝试了什么:



必须创建所有类以及如何在此上下文中使用字典。

我想创建一个类形状,有两个属性作为Name和Date,一个字典,键为Shape名称,值为工作日或周末。

Create a C# library project that calculates the total area of all shapes in a collection.
Details
For simplicity, two types of shapes are required below
Circle
Rectangle
Support of adding different types of shapes in the future with minimum change to code is desired.
Each shape object contains two properties below.
Name
1. The name of a shape, e.g. ‘Red Circle’, ‘Blue Rectangle’, which is provided by callers or users
2. An identity for the type of a shape within a collection. That is, the name of a shape is unique for that type of the shape within a collection.

Creation Date
1. The date when a shape object is created, e.g. 12 Jun 2019
2. Circles are allowed to be created on weekdays, not allowed to be created on weekends.
3. Rectangles are allowed to be created on weekends, not allowed to be created on weekdays
Finally, calculate the total area of all shapes in a collection.

What I have tried:

Which all class have to be created and how to use dictionary in this context.
I would like to create a class shape with two properties as Name and Date and one dictionary with key as Shape name and value as either weekday or weekend.

推荐答案

如果您正在等待许可, m然后考虑给它。



忘记字典和其他收藏品,直到你有实际的课程 - 你不需要担心总数或其他任何东西,直到你将你的形状类排序。



我将从一个抽象的Shape类开始,有一个抽象的Area属性或GetArea方法,一个抽象的Name属性,以及一个具体的CreateDate属性。

然后从中派生你的Circle和Rectangle类,并为每个类实现方法和属性,以及适当的构造函数(检查是否可以在该日期创建类)。



想一想,看看你能走多远。
If you are waiting for permission,m then consider it given.

Forget dictionaries and other collections until you have the actual classes in place - you don't need to worry about totals or anything else until you have your shape classes sorted.

I would start with an abstract Shape class, which has an abstract Area property or GetArea method, and an abstract Name property, and a concrete CreateDate property.
Then derive your Circle and Rectangle classes from that, and implement the methods and properties for each, along with the appropriate constructors (which check if a class can be created on that date).

Think about it, and see how far you get.


除了OriginalGriff的解决方案......



你不需要创建一个形状类型的身份集合的。您需要做的就是检测自定义形状的类型。



详情请见:

继承 - C#编程指南| Microsoft Docs [ ^ ]

多态性 - C#编程指南| Microsoft Docs [ ^ ]

抽象和密封类和类成员 - C#编程指南| Microsoft Docs [ ^ ]



祝你好运!
In addition to OriginalGriff's solution...

You don't need to create "an identity for the type of a shape within a collection". All you need to do is to detect the type of custom shape.

For further details, please see:
Inheritance - C# Programming Guide | Microsoft Docs[^]
Polymorphism - C# Programming Guide | Microsoft Docs[^]
Abstract and Sealed Classes and Class Members - C# Programming Guide | Microsoft Docs[^]

Good luck!


这篇关于创建一个C#库项目,用于计算集合中所有形状的总面积。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆