覆盖属性和继承问题
Overriding properties & inheritance issue
我没有对我的任何模块进行重大更改,但突然开始无法编译我的很多模块。
我收到以下错误
borders.d(190): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
borders.d(191): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\image.d(117): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\image.d(121): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\image.d(121): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(45): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(59): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(73): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(89): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(100): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(110): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\labelbutton.d(78): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\labelbutton.d(89): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\textbox.d(245): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(247): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(259): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(309): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\textbox.d(310): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(324): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\textbox.d(325): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
只是为了说明导致它的原因的示例。这是来自 borders.d
的代码片段
this(Context parent, Paint topPaint, Paint rightPaint, Paint bottomPaint, Paint leftPaint) {
super();
if (!parent)
throw new BorderException("Pass a context to render the border around.");
m_parent = parent;
m_topPaint = topPaint;
m_rightPaint = rightPaint;
m_bottomPaint = bottomPaint;
m_leftPaint = leftPaint;
position = parent.position; // Line 190
size = parent.size; // Line 191
}
现在让我们看看Context.position
和Context.size
override void size(Point newSize) {
super.size = newSize;
if (m_backgroundPaint != transparent) {
m_backgroundShape = new RectangleShape(Vector2f(cast(float)super.width, cast(float)super.height));
m_backgroundShape.fillColor = m_backgroundPaint.toSfmlColor();
m_backgroundShape.position = Vector2f(cast(float)super.x, cast(float)super.y);
}
if (m_border) {
m_border.size = Point(super.size);
}
}
override void position(Point newPosition) {
super.position = newPosition;
if (m_backgroundShape) {
m_backgroundShape.position = Vector2f(cast(float)super.x, cast(float)super.y);
}
if (m_border) {
m_border.position = Point(super.position);
}
}
那些是二传手。
Context
继承了一个名为 Space
的 class,它具有用于位置和大小的 getter/setter。 Context
只覆盖 setter,但 getter 是相同的,应该从 Space
调用。似乎它甚至根本没有从 Space
获得属性。如您所见,它在应该获取 getter 时尝试获取 setter。然而,所有这些代码都可以更早地工作。
这是 Space
中的属性。
Point position() { return m_position; }
void position(Point newPos) {
m_position = newPos;
}
Point size() { return m_size; }
void size(Point newSize) {
m_size = newSize;
}
注意:我将所有属性包装成如下所示,以防万一您想知道为什么找不到 @property
。
@property {
// properties
}
我真的不知道是什么原因导致的,这让我认为这与编译器分析模块的顺序有关?
我仍在尝试自己解决这个问题,但我无法发现问题所在,而且在我更改 Space
中的一些行后一切似乎都崩溃了,但它的 none 成员.
如果您想知道 image.d
、imagebutton.d
、labelbutton.d
和 textbox.d
中的错误是由同一件事引起的。他们都继承了 Context
并且也覆盖了大小/位置。
所以继承基本上是这样的:
x (ex. Image, ImageButton, LabelButton, TextBox etc.)
----Control
--------Context
------------Space
LabelButton
和 ImageButton
继承 Button
继承 Control
。
Control
不会覆盖 Context
中的任何成员,因此所有对 Control
的调用都会直接转到 Context
。 Button
也是一样,除了它有一些处理鼠标事件的内部成员(在Context
中处理,但它调用那个。)
这里有 Control
class 和 Button
class 以防万一。
class Control : Context {
protected:
this(string name) {
super(name);
}
}
...
class Button : Control {
private:
bool m_intersecting = false;
bool m_focused = false;
void handleMouseReleased(Context context, Mouse.Button button) {
m_focused = m_intersecting;
if (m_focused && button == Mouse.Button.Left) {
foreach (event; m_onClick) {
if (event)
event.exec();
}
}
}
void handleMouseMove(Context context, Point position){
m_intersecting = super.intersect(position);
}
protected:
this(string name) {
super(name);
super.onMouseReleased(new MouseClickEvent(&handleMouseReleased));
super.onMouseMove(new PositionEvent(&handleMouseMove));
}
private Action[] m_onClick;
public void onClick(Action event) {
if (!validMessageThread)
throw new MessageException("Cannot register an event outside of the application thread / render thread");
m_onClick ~= event;
}
}
如果您需要更多信息或代码,请告诉我。但是我认为这应该足够了。属性的继承和覆盖 / 属性 调用在早期运行良好。
解决了这个问题,它似乎是一个编译器错误(或者至少它似乎不是正常行为。)
我在 Context
中覆盖了 Space
中 position/size
的 setter,因此编译器从未在 Space
中寻找 getter,而是在 [=12] 中=].
我解决这个问题的方法是覆盖 Context
.
中 Space
的 position/size
的吸气剂
我没有对我的任何模块进行重大更改,但突然开始无法编译我的很多模块。
我收到以下错误
borders.d(190): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
borders.d(191): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\image.d(117): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\image.d(121): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\image.d(121): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(45): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(59): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(73): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(89): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(100): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\imagebutton.d(110): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\labelbutton.d(78): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\labelbutton.d(89): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\textbox.d(245): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(247): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(259): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(309): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\textbox.d(310): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
controls\textbox.d(324): Error: function aeri.context.Context.size (Point newSize) is not callable using argument types ()
controls\textbox.d(325): Error: function aeri.context.Context.position (Point newPosition) is not callable using argument types ()
只是为了说明导致它的原因的示例。这是来自 borders.d
this(Context parent, Paint topPaint, Paint rightPaint, Paint bottomPaint, Paint leftPaint) {
super();
if (!parent)
throw new BorderException("Pass a context to render the border around.");
m_parent = parent;
m_topPaint = topPaint;
m_rightPaint = rightPaint;
m_bottomPaint = bottomPaint;
m_leftPaint = leftPaint;
position = parent.position; // Line 190
size = parent.size; // Line 191
}
现在让我们看看Context.position
和Context.size
override void size(Point newSize) {
super.size = newSize;
if (m_backgroundPaint != transparent) {
m_backgroundShape = new RectangleShape(Vector2f(cast(float)super.width, cast(float)super.height));
m_backgroundShape.fillColor = m_backgroundPaint.toSfmlColor();
m_backgroundShape.position = Vector2f(cast(float)super.x, cast(float)super.y);
}
if (m_border) {
m_border.size = Point(super.size);
}
}
override void position(Point newPosition) {
super.position = newPosition;
if (m_backgroundShape) {
m_backgroundShape.position = Vector2f(cast(float)super.x, cast(float)super.y);
}
if (m_border) {
m_border.position = Point(super.position);
}
}
那些是二传手。
Context
继承了一个名为 Space
的 class,它具有用于位置和大小的 getter/setter。 Context
只覆盖 setter,但 getter 是相同的,应该从 Space
调用。似乎它甚至根本没有从 Space
获得属性。如您所见,它在应该获取 getter 时尝试获取 setter。然而,所有这些代码都可以更早地工作。
这是 Space
中的属性。
Point position() { return m_position; }
void position(Point newPos) {
m_position = newPos;
}
Point size() { return m_size; }
void size(Point newSize) {
m_size = newSize;
}
注意:我将所有属性包装成如下所示,以防万一您想知道为什么找不到 @property
。
@property {
// properties
}
我真的不知道是什么原因导致的,这让我认为这与编译器分析模块的顺序有关?
我仍在尝试自己解决这个问题,但我无法发现问题所在,而且在我更改 Space
中的一些行后一切似乎都崩溃了,但它的 none 成员.
如果您想知道 image.d
、imagebutton.d
、labelbutton.d
和 textbox.d
中的错误是由同一件事引起的。他们都继承了 Context
并且也覆盖了大小/位置。
所以继承基本上是这样的:
x (ex. Image, ImageButton, LabelButton, TextBox etc.)
----Control
--------Context
------------Space
LabelButton
和 ImageButton
继承 Button
继承 Control
。
Control
不会覆盖 Context
中的任何成员,因此所有对 Control
的调用都会直接转到 Context
。 Button
也是一样,除了它有一些处理鼠标事件的内部成员(在Context
中处理,但它调用那个。)
这里有 Control
class 和 Button
class 以防万一。
class Control : Context {
protected:
this(string name) {
super(name);
}
}
...
class Button : Control {
private:
bool m_intersecting = false;
bool m_focused = false;
void handleMouseReleased(Context context, Mouse.Button button) {
m_focused = m_intersecting;
if (m_focused && button == Mouse.Button.Left) {
foreach (event; m_onClick) {
if (event)
event.exec();
}
}
}
void handleMouseMove(Context context, Point position){
m_intersecting = super.intersect(position);
}
protected:
this(string name) {
super(name);
super.onMouseReleased(new MouseClickEvent(&handleMouseReleased));
super.onMouseMove(new PositionEvent(&handleMouseMove));
}
private Action[] m_onClick;
public void onClick(Action event) {
if (!validMessageThread)
throw new MessageException("Cannot register an event outside of the application thread / render thread");
m_onClick ~= event;
}
}
如果您需要更多信息或代码,请告诉我。但是我认为这应该足够了。属性的继承和覆盖 / 属性 调用在早期运行良好。
解决了这个问题,它似乎是一个编译器错误(或者至少它似乎不是正常行为。)
我在 Context
中覆盖了 Space
中 position/size
的 setter,因此编译器从未在 Space
中寻找 getter,而是在 [=12] 中=].
我解决这个问题的方法是覆盖 Context
.
Space
的 position/size
的吸气剂