[英]How do I pass a copy constructor as a method reference?
I have a class PlanItemEditor. 我有一个PlanItemEditor类。
I require the E item's copy constructor. 我需要E项的复制构造函数。 How do I pass it in using a method reference?
如何使用方法引用传递它?
public void initValues(ObservableList<E> srcList, E toEdit) {
indexToSet = srcList.indexOf(toEdit);
editing = new E(toEdit);
this.srcList = srcList;
}
I was thinking of including a Unary function as a parameter, and passing in a method from the E class that was exactly a copy constructor, but with a different name signature. 我正在考虑将一个Unary函数作为参数包含在内,并从E类中传入一个完全是复制构造函数但具有不同名称签名的方法。 It's kind of hacky, however.
然而,这有点像hacky。
Is there a better way of doing this? 有没有更好的方法呢?
You can use: 您可以使用:
public void initValues(ObservableList<E> srcList, E toEdit, UnaryOperator<E> copy) {
indexToSet = srcList.indexOf(toEdit);
editing = copy.apply(toEdit);
this.srcList = srcList;
}
And you can call it with: 你可以用它来调用它:
initValues(srcList, toEdit, YourClass::new);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.