继承问题
我有一个绘图程序,用户可以绘制椭圆或直线,它们都源自形状。我正在创建一个橡皮筋,根据用户正在绘制的内容,我说,
rubberBand = new Ellipse();
//or
rubberBand = new Line();
但如果我将橡皮筋设置为线条,我将无法访问 x1 x2 等,它表示形状不包含 X1 的定义。我尝试创建一个椭圆并将其投射到一条线上,但仍然存在同样的问题。我该如何解决这个问题?
I have drawing program that the user can draw either an ellipse or a line, which both derive from shape. I am creating one rubber band, and depending on what the user is drawing i say
rubberBand = new Ellipse();
//or
rubberBand = new Line();
but if i set the rubber band to line, I cannot access the x1 x2 etc, it says shape does not contain a definition of X1. I tried creating an Ellipse and the casting it to a line but still same issue. How do I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
卖梦商人2024-12-08 15:13:26
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
对我来说,这听起来像是一个基本的多态性问题。想想您实际上想要做什么 - 例如,一条线有 2 个点(X1/Y1 和 X2/Y2)。椭圆(长方形圆)没有这样的属性 - 它可能有宽度和高度,还可能有 X 和 Y 坐标(或位置属性)。
我猜测当用户用鼠标拖动形状时,您正在尝试调整形状的边界和/或位置。在这种情况下,您需要为形状定义的操作取决于它是什么类型的形状。对于一条线,您需要编写一个调整 X2 和 Y2 (或其他)的方法。对于椭圆形,您可能需要另一种方法来调整具有宽度、高度、左侧和顶部属性的形状。然后,您只需根据要处理的形状来确定调用哪一个即可。
This sounds like a basic polymorphism question to me. Think about what are you actually trying to do- for instance, a line has 2 points (X1/Y1, and X2/Y2). An ellipse (an oblong circle) has no such property- it has a width, maybe, and a height, and possibly an X and Y coordinates (or a position property).
I am guessing that you are attempting to adjust the bounds and/or location of the shape when the user is dragging it with the mouse. In this case, the operations that you need to define for the shape depend on what kind of shape it is. For a line, you need to write a method that adjusts X2 and Y2 (or whatever). For an ellipse, you will probably need another method that adjusts shapes that have width, height, left, and top properties. Then you just need to determine which one to call depending on which kind of shape you are dealing with.