我有一个班级:
public class Shape extends PShape{
private String name;
private PApplet drawer;
public Shape(PApplet drawer, String name){
//constructor
this.drawer = drawer;
this.name = name;
}
}
如果我有
PShape s;
我愿意
s = drawer.createShape();//return PShape
但是,PShape实际上没有构造函数,只是一个返回PShape的方法createShape。
如果我想扩展Shape
,我会在PShape
的构造函数中添加什么内容?
this = drawer.createShape();
那会有用吗?如果没有,我将如何初始化扩展Shape
的<{1}}?
答案 0 :(得分:2)
除了您提供的答案外,您还可以考虑选择composition instead of inheritance。
基本上:您可以创建一个包含一个PShape
实例的类,而不是扩展PShape
。像这样:
public class Shape{
private PShape myShape;
private String name;
private PApplet drawer;
public Shape(PApplet drawer, String name){
//constructor
this.drawer = drawer;
this.name = name;
myShape = drawer.createShape();
}
}
然后您只需在需要时使用该PShape
实例。
答案 1 :(得分:1)
我查看了github @ https://github.com/processing
上的处理源代码我查看了这些文件:
PApplet.java
PGraphics.java
PShape.java
在 https://github.com/processing/processing/blob/master/core/src/processing/core/ 文件夹中。
看起来PShape有一个构造函数:
public PShape(PGraphics g, int family)
因此,以下内容应该放在构造函数中:
super(drawer.g, GEOMETRY); // GROUP and PATH work as well