Java Interfaces/Callbacks 用于使用 2 种可能方法中的 1 种
Java Interfaces/Callbacks for Using 1 of 2 Possible Methods
我已经阅读了 Java 接口(回调),因为一位教授告诉我应该在我的一个程序中使用回调。在我的代码中,有两个数学函数可以 'pick' 使用。当我想更改函数时,他说我应该使用回调,而不是创建方法 activate() 并更改内部代码(从一个函数到另一个函数)。但是,从我读到的有关回调的内容来看,我不确定这有什么用。
编辑:添加了我的代码
public interface
//the Interface
Activation {
double activate(Object anObject);
}
//one of the methods
public void sigmoid(double x)
{
1 / (1 + Math.exp(-x));
}
//other method
public void htan(final double[] x, final int start,
final int size) {
for (int i = start; i < start + size; i++) {
x[i] = Math.tanh(x[i]);
}
}
public double derivativeFunction(final double x) {
return (1.0 - x * x);
}
}
如果你想使用类似这样的界面。
我有一个 MathFunc
接口,它有一个 calc
方法。
在程序中,我有一个 MathFunc
用于乘法,一个用于加法。
使用方法 chooseFunc
您可以选择两者之一,使用 doCalc
当前选择的 MathFunc
将进行计算。
public interface MathFunc {
int calc(int a, int b);
}
你可以这样使用它:
public class Program {
private MathFunc mult = new MathFunc() {
public int calc(int a, int b) {
return a*b;
}
};
private MathFunc add = new MathFunc() {
public int calc(int a, int b) {
return a+b;
}
};
private MathFunc current = null;
// Here you choose the function
// It doesnt matter in which way you choose the function.
public void chooseFunc(String func) {
if ("mult".equals(func))
current = mult;
if ("add".equals(func))
current = add;
}
// here you calculate with the chosen function
public int doCalc(int a, int b) {
if (current != null)
return current.calc(a, b);
return 0;
}
public static void main(String[] args) {
Program program = new Program();
program.chooseFunc("mult");
System.out.println(program.doCalc(3, 3)); // prints 9
program.chooseFunc("add");
System.out.println(program.doCalc(3, 3)); // prints 6
}
}
我已经阅读了 Java 接口(回调),因为一位教授告诉我应该在我的一个程序中使用回调。在我的代码中,有两个数学函数可以 'pick' 使用。当我想更改函数时,他说我应该使用回调,而不是创建方法 activate() 并更改内部代码(从一个函数到另一个函数)。但是,从我读到的有关回调的内容来看,我不确定这有什么用。
编辑:添加了我的代码
public interface
//the Interface
Activation {
double activate(Object anObject);
}
//one of the methods
public void sigmoid(double x)
{
1 / (1 + Math.exp(-x));
}
//other method
public void htan(final double[] x, final int start,
final int size) {
for (int i = start; i < start + size; i++) {
x[i] = Math.tanh(x[i]);
}
}
public double derivativeFunction(final double x) {
return (1.0 - x * x);
}
}
如果你想使用类似这样的界面。
我有一个 MathFunc
接口,它有一个 calc
方法。
在程序中,我有一个 MathFunc
用于乘法,一个用于加法。
使用方法 chooseFunc
您可以选择两者之一,使用 doCalc
当前选择的 MathFunc
将进行计算。
public interface MathFunc {
int calc(int a, int b);
}
你可以这样使用它:
public class Program {
private MathFunc mult = new MathFunc() {
public int calc(int a, int b) {
return a*b;
}
};
private MathFunc add = new MathFunc() {
public int calc(int a, int b) {
return a+b;
}
};
private MathFunc current = null;
// Here you choose the function
// It doesnt matter in which way you choose the function.
public void chooseFunc(String func) {
if ("mult".equals(func))
current = mult;
if ("add".equals(func))
current = add;
}
// here you calculate with the chosen function
public int doCalc(int a, int b) {
if (current != null)
return current.calc(a, b);
return 0;
}
public static void main(String[] args) {
Program program = new Program();
program.chooseFunc("mult");
System.out.println(program.doCalc(3, 3)); // prints 9
program.chooseFunc("add");
System.out.println(program.doCalc(3, 3)); // prints 6
}
}