调用未在java中实现的接口函数

时间:2018-04-11 06:57:08

标签: java

我无法理解使用接口函数的代码,但我似乎无法找到它的实现。是否可以调用未实现的接口函数?代码如下, 调用未实现函数的类combineResults:

public final class Parallel { 
 public static <T> T For(int start, int end, 
   ParallelForDelegate<T> delegate, T input) { 
  T res = null; 
  for (int i = start; i < end; i++) { 
   res = delegate.combineResults(res, delegate.delegate(i, input)); 
  } 
  return res; 
 } 
 }

界面:

public  interface  ParallelForDelegate<T> { 
 public  T delegate(int i, T input); 

 public  T combineResults(T result1, T result2); 
}

参考: https://www.programcreek.com/java-api-examples/index.php?source_dir=SecugenPlugin-master/src/sourceafis/simple/Fingerprint.java#

2 个答案:

答案 0 :(得分:1)

必须在某个地方实施,但有时你可能很难找到它,因为有各种工厂,依赖性注射,以及“飞行中”。建筑,... mecanisms

在像Hibernate这样的复杂API上,它可能很乏味。但是,该界面应该为您提供足够的使用细节,因此您实际上 知道它是如何实现的。

答案 1 :(得分:1)

方法

  

public static T For(int start,int end,ParallelForDelegate   代表,T输入)

并行类的

您将ParallelForDelegate作为参考,因此,您可以调用ParallelForDelegate接口的方法。 关键是你调用For( - , - , - , - )方法时必须传递ParallelForDelegate接口的实现类对象。所以,

  

delegate.combineResults(res,delegate.delegate(i,input));

调用,即运行时的实现类方法。