2015-11-17 33 views
2

我有一個類:繼承PShape

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並沒有真正有一個構造函數,只是一個方法createShape返回一個PShape。

如果我想擴展PShape,我會在Shape的構造函數中放什麼?

this = drawer.createShape(); 

會這樣嗎?如果不是,我將如何初始化一個Shape,延伸PShape?在https://github.com/processing/processing/blob/master/core/src/processing/core/文件夾

PApplet.java 
PGraphics.java 
PShape.java 

回答

2

除了您所提供的答案,你可能會考慮選擇composition instead of inheritance

基本上:而不是擴展PShape,您將創建一個類包含 a 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

我看着處理GitHub上的源代碼@https://github.com/processing

我看着這些文件。

它看起來像PShape有一個構造函數:

public PShape(PGraphics g, int family) 

因此,下面的構造應該去:

super(drawer.g, GEOMETRY); // GROUP and PATH work as well