简体   繁体   English

将方法传递给方法的解决方法?

[英]Workaround for passing a method into a method?

I am looking for a way to pass a method as a parameter into another method. 我正在寻找一种将方法作为参数传递给另一种方法的方法。

I am currently trying to simulate the Newton's-method ( http://en.wikipedia.org/wiki/Newton%27s_method ) in Java with the following code: 我目前正在尝试使用以下代码在Java中模拟牛顿方法( http://en.wikipedia.org/wiki/Newton%27s_method ):

public class Newton {


// Iterationmethod
public void newtonCalc(double x0) {
    double x;

    // counter
    int i = 0;

    //Newton-Iteration for x0
    x = x0 - (y2(x0) / y2Deriv(x0));

    while (Math.sqrt(y2(x)*y2(x)) >= Math.pow(10, -10)){
        //Newton-Iteration x(n+1)
        x = x - (y2(x))/ y2Deriv(x);
        i++;
        System.out.printf("%d. %.11f\n",i,y2(x));
    }

    System.out.printf("%d steps were necessary for a resolution of 10^-10", i);
}

// Function for (2)
public static double y2(double x) {
    return Math.sin(x) / (1 - Math.tan(x));
}

// Derivative for (2)
public static double y2Deriv(double x) {
    return (Math.cos(x) + Math.sin(x) * Math.tan(x) * Math.tan(x))
            / ((Math.tan(x) - 1) * (Math.tan(x) - 1));
}

// Function for (4)
public static double y4(double x) {
    return Math.exp(-1/Math.sqrt(x));
}

// Derivative for (4)
public static double y4Deriv(double x) {
    return Math.exp(-1/Math.sqrt(x))/(2*Math.pow(x, 3d/2));
}



public static void main(String[] args) {
    Newton newton = new Newton();
    newton.newtonCalc(1);
}

} }

newtonCalc(x0) gets an x0 at wich the iteration should be started. newtonCalc(x0)在应开始迭代的时刻获取x0。 But the function (y2) now is hardcoded into this method. 但是函数(y2)现在被硬编码到该方法中。 I want it to be flexible. 我希望它具有灵活性。 For example newtonCalc(double x0, Method y) to run the iteration for y starting at x0. 例如newtonCalc(double x0,方法y)从x0开始为y运行迭代。 I have 2 different functions (y2 and y4 which are both functions from a excercise sheet from my lecture plus its derivatives y2Deriv and y4Deriv which are used in the Iterationmethod). 我有2个不同的函数(y2和y4都是我的演讲练习表中的函数,以及在Iterationmethod中使用的其派生y2Deriv和y4Deriv)。

I know passing a method is not possible but i dont get any easy workaround. 我知道通过方法是不可能的,但是我没有任何简单的解决方法。

Forgive me if this is unclear or i have missed any necessary information! 如果不清楚,请原谅我,否则我错过了任何必要的信息!

Regards, 问候,

Tak3r07 Tak3r07

从Java 8开始,完全有可能使用lambda表达式

If you want to stay with pre-Java-8 methods, make an interface Function with member functions eval and deriv and pass (possibly anonymous) instances of derived classes to the Newton class invocation. 如果你想留在前期的Java-8的方法,将界面Function与成员函数evalderiv并通过派生类的(可能是匿名)实例牛顿类调用。


This could look like (I'm not sure that all details are correct, these are just code fragments to illustrate the idea) 这可能看起来像(我不确定所有细节是否正确,这些只是用于说明此想法的代码片段)

interface Function {
     public double eval(double x);
     public double deriv(double);
}

class Example1 implements Function {
     @override
     public double eval(double x) { return x*(x+3)+1; }
     @override
     public double deriv(double) { return 2*x+3; }
}

....

Solver solver1 = new Newton(new Example1(),x0);
....
Solver solver2 = new Newton(new Function(){
     @override
     public double eval(double x) { return cos(x); }
     @override
     public double deriv(double) { return -sin(x); }
}, x0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM