2017-10-04 50 views
-6

我在调用其他类时遇到了一些使用函数的问题。Java使用函数

主要类:

public static void main(String [] args) { 
    baseIteration(new Function() { 
     public double calculate(double x) { 
      return Math.sin(x) - Math.log10(x); 
     } 

     @Override 
     public Object apply(Object t) { 
      // TODO Auto-generated method stub 
      return null; 
     } 
    }.calculate(double x),0,10.0); 
} 

而且baseIteration功能:

public static void baseIteration(Function f, double a, double b) { 
    double prev = f.calculate(a); 
    double step = (b-a)/10; 
    for(double p = a+step; p<=b+10e-8; p+=step) { 
     double curr = f.calculate(p); 
    } 
} 

这还不是全部,但主要的问题:f.calculation是不确定的!

回答

0

那么,Function没有calculate方法,它只有apply方法。而且您通常不需要明确实施Function

尝试以下操作:

public static void main(String [] args) { 
    baseIteration(x -> Math.sin(x) - Math.log10(x), 0, 10.0); 
} 

public static void baseIteration(Function<Double, Double> f, double a, double b) { 
    double prev = f.apply(a); 
    double step = (b-a)/10; 
    for(double p = a+step; p<=b+10e-8; p+=step) { 
     double curr = f.apply(p); 
    } 
} 
+0

它开始工作!非常感谢!现在算法有问题,但是它开始工作了!谢谢! – Maksym