继承PShape
Inheriting PShape
我有一个 class:
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 并没有真正的构造函数,只有一个 returns 一个 PShape 的方法 createShape。
如果我想扩展 PShape
,我会在 Shape
的构造函数中放入什么?
this = drawer.createShape();
这样行吗?如果没有,我将如何初始化扩展 PShape
的 Shape
?
我查看了 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
除了您提供的答案外,您还可以考虑选择 composition instead of inheritance。
基本上:不是扩展 PShape
,而是创建一个 class,其中 包含 一个 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
实例。
我有一个 class:
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 并没有真正的构造函数,只有一个 returns 一个 PShape 的方法 createShape。
如果我想扩展 PShape
,我会在 Shape
的构造函数中放入什么?
this = drawer.createShape();
这样行吗?如果没有,我将如何初始化扩展 PShape
的 Shape
?
我查看了 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
除了您提供的答案外,您还可以考虑选择 composition instead of inheritance。
基本上:不是扩展 PShape
,而是创建一个 class,其中 包含 一个 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
实例。