Java简化了绘制对象路径



所以,基本上,我正在尝试制作一个函数接口,这样我就可以在不同的绘制操作之间进行选择,而不必写出传递给每个操作的实际变量。我有这个:

public void draw(Graphics g){
setColor(g);
Consumer4 c;
//if (getSolid()) g.fillOval(this.x, this.y, this.width, this.height);
//else g.drawOval(this.x, this.y, this.width, this.height);
if (getSolid()) c = (int t,int u,int v,int w) -> g.fillOval(t,u,v,w);
else c = (t,u,v,w) -> g.drawOval(t,u,v,w);
g.c(this.x, this.y, this.width, this.height);
}

我创建了这个类来帮助它:(注意它位于一个抽象的超类中(

@FunctionalInterface
protected static interface Consumer4{
//public abstract <T,U,V,W>void accept(T t,U u,V v,W w);
public abstract void accept(int t,int u,int v,int w);
}

注释掉的部分是我希望它做的,也注意到我试图用泛型来做,因此我的函数接口的注释掉部分。

我也试过这样做,看看它是否会有所帮助,以及我的功能接口中的相关更改,但没有成功。

if (getSolid()) c = (int t,int u,int v,int w,Graphics y) -> y.fillOval(t,u,v,w);
else c = (t,u,v,w,y) -> y.drawOval(t,u,v,w);
c(this.x, this.y, this.width, this.height,g);

这可行吗?或者我做错了什么。

Nvm,我发现了。。。

if (getSolid()) c = (int t,int u,int v,int w) -> g.fillOval(t,u,v,w);
else c = (t,u,v,w) -> g.drawOval(t,u,v,w);
c.accept(this.x, this.y, this.width, this.height);

不过,不管我怎么想,它似乎比它的价值更多的代码,除非c被多次使用。

最新更新