我正在学习Lambda,我遇到了一些我无法解决的问题。
最初我的代码是:
package Lambdas;
@FunctionalInterface
interface NumericFunc2 {
<T extends Number> T func(T n);
}
class Lbd_488_NumericFunc_SelfTest {
public static void main(String args[]) {
// This block lambda returns the smallest positive factor of a value.
NumericFunc2 smallestF = (n) -> {
double result = 1;
// Get absolute value of n.
n = n < 0 ? -n : n;
for (int i = 2; i <= n / i; i++)
if ((n % i) == 0) {
result = i;
break;
}
return result;
};
System.out.println("Smallest factor of 12 is " + smallestF.func(12));
System.out.println("Smallest factor of 11 is " + smallestF.func(11));
}
}然而,在我的操作符(**<**, -**,* /**).旁边,一直存在错误。PS:,即使我将其更改为**,我也会得到相同的错误。
现在,如果我通过添加参数类型来更改代码,就会得到一个错误,即“目标方法是泛型的”
即使我将其更改为<T extends Double> ,也会得到相同的错误。
package Lambdas;
@FunctionalInterface
interface NumericFunc2 {
<T extends Number> T func(T n);
}
class Lbd_488_NumericFunc_SelfTest {
public static void main(String args[]) {
// This block lambda returns the smallest positive factor of a value.
NumericFunc2 smallestF = (Double n) -> {
double result = 1;
// Get absolute value of n.
n = n < 0 ? -n : n;
for (int i = 2; i <= n / i; i++)
if ((n % i) == 0) {
result = i;
break;
}
return result;
};
System.out.println("Smallest factor of 12 is " + smallestF.func(12));
System.out.println("Smallest factor of 11 is " + smallestF.func(11));
}
}但是,如果我将类从非泛型更改为泛型,那么所有的都可以很好地工作,就像下面的代码:
package Lambdas;
@FunctionalInterface
interface NumericFunc2<T extends Number> {
T func(T n);
}
class Lbd_488_NumericFunc_SelfTest {
public static void main(String args[]) {
// This block lambda returns the smallest positive factor of a value.
NumericFunc2<Double> smallestF = (n) -> {
double result = 1;
// Get absolute value of n.
n = n < 0 ? -n : n;
for (int i = 2; i <= n / i; i++)
if ((n % i) == 0) {
result = i;
break;
}
return result;
};
System.out.println("Smallest factor of 12 is " + smallestF.func(12));
System.out.println("Smallest factor of 11 is " + smallestF.func(11));
}
}所以我的问题是。我在代码的第一部分做些什么?如何正确地在非泛型类中与泛型方法一起使用lambda?
发布于 2016-07-18 00:54:52
答案是您不能对Number对象执行这种数字操作。不可能;这与lambda无关,与Number 抽象类有任何关系,只是没有提供这些特性。
在不知道Number是哪种Number的情况下,甚至没有任何方法可以得到它的绝对值。你看不出它是正的还是负的,你不能否定它,你不能加,减,或者乘以它。使用Number所能做的唯一的事情就是将它转换成另一种原始类型,但是您不能将它转换回来,也不能用它进行数学运算。
发布于 2016-07-18 11:10:01
根据JLS,泛型和lambda不会一起工作。这个eclipse错误报告用一些代码示例来讨论它。
为了使它工作,您必须回到匿名内部类。也就是说,您必须编写完整的new NumberFunc() {...} mambo围绕您的功能。
我不得不对该方法的正文进行一些其他修改,以使其编译:
result是盒式Double,所以我可以将它转换为TT extends Number上工作。把它组合在一起:
class Lbd_488_NumericFunc_SelfTest {
public static void main(String args[]) {
// This block lambda returns the smallest positive factor of a value.
NumericFunc2 smallestF = new NumericFunc2() {
public <T extends Number> T func(T n) {
Double result = 1d;
// Get absolute value of n.
double nAbs = n.doubleValue() < 0 ? -n.doubleValue() : n.doubleValue();
for (int i = 2; i <= nAbs / i; i++)
if ((nAbs % i) == 0) {
result = (double) i;
break;
}
return (T) result;
}
};
System.out.println("Smallest factor of 12 is " + smallestF.func(12));
System.out.println("Smallest factor of 11 is " + smallestF.func(11));
}
}https://stackoverflow.com/questions/38427291
复制相似问题