如何使用 Java/Processing 调用子类的方法?
How to call method on subclass with Java/Processing?
在我的 Sketch 中,我有一个 UI 对象数组。数组中是一个 Frame 对象。 Frame 是继承自 UI class 的 class。我试图调用 Frame 对象中的方法,但出现错误 'The function AddChild(MyScratch.Frame) does not exist'。现在,我可以通过将 UI 对象转换为 Frame 然后调用该方法来修复此错误,但我不想一直这样做。有没有什么办法解决这一问题?谢谢
代码如下:
private UI[] uis = new UI[] {
new Frame(10, 10, 100, 100)
};
public void setup() {
size(600, 600);
// call the AddChild(UI u) method of Frame, causes an error
// I know I could do ((Frame)uis[0]).AddChild(...), but I don't
// want to do that constantly
uis[0].AddChild(new Frame(0, 0, 100, 100));
}
public void draw() {
}
由于方法 AddChild
不在 UI
class 中,您不能通过引用 UI
class 来调用此函数.
您可以选择以下其中一项:
- 将
AddChild
添加到 UI
class
- 使用
Frame
数组代替 UI
数组
- 检查
UI
数组中的实例是否为 Frame
并转换它。
在我的 Sketch 中,我有一个 UI 对象数组。数组中是一个 Frame 对象。 Frame 是继承自 UI class 的 class。我试图调用 Frame 对象中的方法,但出现错误 'The function AddChild(MyScratch.Frame) does not exist'。现在,我可以通过将 UI 对象转换为 Frame 然后调用该方法来修复此错误,但我不想一直这样做。有没有什么办法解决这一问题?谢谢
代码如下:
private UI[] uis = new UI[] {
new Frame(10, 10, 100, 100)
};
public void setup() {
size(600, 600);
// call the AddChild(UI u) method of Frame, causes an error
// I know I could do ((Frame)uis[0]).AddChild(...), but I don't
// want to do that constantly
uis[0].AddChild(new Frame(0, 0, 100, 100));
}
public void draw() {
}
由于方法 AddChild
不在 UI
class 中,您不能通过引用 UI
class 来调用此函数.
您可以选择以下其中一项:
- 将
AddChild
添加到UI
class - 使用
Frame
数组代替UI
数组 - 检查
UI
数组中的实例是否为Frame
并转换它。