【发布时间】:2020-12-06 03:06:11
【问题描述】:
所以,基本上,我正在尝试制作一个函数式接口,以便我可以在不同的绘制操作之间进行选择,而不必写出传递给每个绘制操作的实际变量。我有这个:
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);
这可行吗?或者我做错了什么。
【问题讨论】:
标签: java methods functional-interface