C# 形状层次结构编程语言
创建一个应用程序,该应用程序使用对层次结构中每个具体类的对象的 Shape 引用数组。应用程序应打印文本描述 每个数组元素引用的对象的。另外,在处理数组中所有形状的循环中,确定每个形状是 TwoDimensionalShape 还是 ThreeDimensionalShape。如果形状是 TwoDimensionalShape,则显示其面积。如果形状是 ThreeDimensionalShape,则显示其面积和体积。
我是这里真正的新手,我真的迷失了这样做。请有人帮我解决这个问题。谢谢。
Create an application that uses an array of Shape references to objects of each concrete class in the hierarchy. The application should print a text description
of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If a shape is a TwoDimensionalShape, display its area. If a shape is a ThreeDimensionalShape, display its area and volume.
I am a real newbie here, I'm really lost doing this. Please, could someone help me with this. Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也一直被这个问题困扰,但在 Youtube 上找到这个抽象类教程后,一切都变得清晰起来: http://www.youtube.com/watch?v=hwKOMfsYyxo
教科书中的问题对于需要抽象什么内容不是很清楚,但是当你查看上下文时,Shape、TwoDimensionalShape 和ThreeDimensionalShape 必须全部是抽象的,并且其中包含 Area 和 Volume 的抽象方法。尽管我发现的技巧是,一旦声明了抽象方法,就不必在派生抽象类中声明该方法,直到具体类实现它们为止。
I've been stuck on this one as well, but after finding this abstract class tutorial on Youtube, it all became clear: http://www.youtube.com/watch?v=hwKOMfsYyxo
The question in the textbook wasn't very clear on what needs to abstract but when you look at the context, Shape, TwoDimensionalShape, and ThreeDimensionalShape have to all be abstract and contain abstract methods for Area and Volume within them. Though the trick that I discovered was that once you declare an abstract method, you don't have to declare that method in derived abstract classes until a concrete class implements them.