在java中将匿名类更改为普通类
我在java中使用了一个匿名的内部类,它在其他几个函数中实现,我已经尝试将它变成一个普通类,因此C++中更容易转换。 这是我的密码
interface CombineFunction {
void combine(int[] target, int[] source);
}
class CombineLocals {
public int[] Vector;
public int[] SrcVector;
}
void Combine(final BinaryMap source, final RectangleC area, final Point at,
final CombineFunction function) {
final int shift = (area.X & WordMask) - (at.X & WordMask);
int vectorSize = (area.Width >> WordShift) + 2;
CombineLocals combineLocals = new CombineLocals();
combineLocals.Vector = new int[vectorSize];
combineLocals.SrcVector = new int[vectorSize];
ParallelForDelegate<CombineLocals> delegate = new ParallelForDelegate<CombineLocals>() {
@Override
public CombineLocals delegate(int y, CombineLocals locals) {
LoadLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
source.LoadLine(locals.SrcVector,
new Point(area.X, area.Y + y), area.Width);
if (shift >= 0)
ShiftLeft(locals.SrcVector, shift);
else
ShiftRight(locals.SrcVector, -shift);
function.combine(locals.Vector, locals.SrcVector);
SaveLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
return locals;
}
@Override
public CombineLocals combineResults(CombineLocals result1,
CombineLocals result2) {
return null;
}
};
Parallel.For(0, area.Height, delegate, combineLocals);
}
正如上面给出的代码一样,ParallelForDelegate也是另一个像匿名内部类一样使用的接口。 我试图更改代码如下:
class Combine{
BinaryMap source;
RectangleC area;
Point at;
Combine(BinaryMap bm, RectangleC rc, Point p){
source = bm;
area = rc;
at = p;
}
void combine(int[] target, int[] source){
final int shift =(area.X & WordMask) - (at.X & WordMask);
int vectorSize = (area.Width >> WordShift) + 2;
CombineLocals combineLocals = new CombineLocals();
combineLocals.Vector = new int[vectorSize];
combineLocals.SrcVector = new int[vectorSize];
Parallel.For(0, area.Height, delegate(), combineLocals);
}
public CombineLocals delegate(int y, CombineLocals locals, int shift){
LoadLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
source.LoadLine(locals.SrcVector,
new Point(area.X, area.Y + y), area.Width);
if (shift >= 0)
ShiftLeft(locals.SrcVector, shift);
else
ShiftRight(locals.SrcVector, -shift);
combine(locals.Vector, locals.SrcVector);
SaveLine(locals.Vector, new Point(at.X, at.Y + y), area.Width);
return locals;
}
}
然而,当我试图并行调用委托函数时。对于函数,我不知道把什么作为参数。
原始类取自此SDK
https://www.programcreek.com/java-api-examples/index.php?source_dir=SecugenPlugin-master/src/sourceafis/simple/Fingerprint.java#
# 1 楼答案
函数
Parallel.For
需要一个ParallelForDelegate<CombineLocals>
类型的项。这意味着你别无选择:你必须创建ParallelForDelegate<CombineLocals>
类型的东西如果不想使用匿名内部类,那么需要创建一个实现接口的命名类。由于这个类将不在与原始内部类相同的范围内,因此需要找到一种方法来传递适当的值
您还需要包含mutator方法和/或构造函数来设置私有字段的值。然后您可以实现
Combine
方法,如下所示如果在C++中实现这一点太难了,那么你需要找到另一种编写代码的方法。p>
注意:您不应该调用Java方法
Combine
;取而代之的是combine