我正在使用EJML,我想使用类LinearSolver_B64_to_D64
,它具有构造函数:LinearSolver_B64_to_D64(LinearSolver<BlockMatrix64F> alg)
与接口LinearSolver<BlockMatrix64F>
,并且该类已经实现了LinearSolver
。如何使用界面作为参数?
我所知道的: 一般来说,你创建一个接口,比你会实现在一个特定的类接口。 我读过关于将接口作为参数的函数(在特定的类中),因为这样你的函数就不需要知道关于类的东西了。
我的问题: 我不知道如何初始化类LinearSolver_B64_to_D64
,因为我不知道怎么的接口作为参数传递。
更新: 我尝试下面的代码:
public class UseMatrixInterface{
public UseMatrixInterface(){
}
public void do1(){
DenseMatrix64F a = new DenseMatrix64F(3,3);
LinearSolver_B64_to_D64 ls = new LinearSolver_B64_to_D64(null);
//it throws a nullpointer exeption. I assume, it is because i used null
//instead of the requiered parameter.
ls.invert(a);
a.print();
}
public void do2(){
LinearSolver<BlockMatrix64F> lsD;
LinearSolver_B64_to_D64 ls = new LinearSolver_B64_to_D64(lsD);
//not working, because lsD cannot be initialised;
}
}
您需要将接口的实现作为实际值传递。 –
您需要一个实现LinearSolver接口的类的实例。 –