更优雅的内部方法声明(没有所有的混乱)?
More elegant inner methods declarations (without all the clutter)?
我有一个递归的方法,用某种算法计算x^n,但这在这里并不重要。重要的是我的辅助函数,它跟踪该算法的递归调用。
public class FastPot {
public static double fastPotRek(double x, int n) {
class Aux {
private double aux(double x, int n, int c) {
if (n == 0) {System.out.print("It took "+c+" recursive calls to compute "); return 1;}
else return (n % 2 == 0) ? aux(x*x, n/2, c+1)
: x * aux(x, n-1, c+1);
}
}
Aux a = new Aux();
return a.aux(x, n, 0);
}
}
为了稍微组织一下,我想在 fastPotRek
内声明 aux
,为此您必须使用内部 class,我不能将其方法声明为静态的。因此,我实例化 Aux a = new Aux();
以便能够调用 aux
.
请告诉我有一种方法可以使它更优雅,并告诉我我忽略了什么......就像蜜蜂能够以某种方式使 aux
静态或不需要实例化 Aux
。
不需要内部 class 也不需要使其成为静态的:
public class FastPot {
//Static, use only from within FastPot
private static double aux(double x, int n, int c) {
if (n == 0) {
System.out.print("It took "+c+" recursive calls to compute ");
return 1;
} else {
return (n % 2 == 0) ? aux(x*x, n/2, c+1)
: x * aux(x, n-1, c+1);
}
}
}
//Your outward interface
public static double fastPotRek(double x, int n) {
return aux(x, n, 0);
}
}
或者如果你坚持使用内部 class:
public class FastPot {
//Static, use only from within FastPot
private static class Aux {
private static double aux(double x, int n, int c) {
if (n == 0) {
System.out.print("It took "+c+" recursive calls to compute ");
return 1;
} else {
return (n % 2 == 0) ? aux(x*x, n/2, c+1)
: x * aux(x, n-1, c+1);
}
}
}
//Your outward interface
public static double fastPotRek(double x, int n) {
return Aux.aux(x, n, 0);
}
}
你有这个:
Aux a = new Aux();
return a.aux(x, n, 0);
我会这样写(一样):
return new Aux().aux(x, n, 0);
编辑: 我用完了 Whosebug。你们不需要帮助吗?我不会给它。以后我只问我的个问题,不回答
我会 post 这个答案,尽管很多人(包括我)对此并不满意。请不要使用这种代码:
public static double fastPotRek(double x, int n) {
return new Cloneable() {
private double aux(double x, int n, int c) {
if (n == 0) {System.out.print("It took "+c+" recursive calls to compute "); return 1;}
else return (n % 2 == 0) ? aux(x*x, n/2, c+1) : x * aux(x, n-1, c+1);
}
}.aux(x, n, 0);
}
同样,我强烈建议使用私有静态方法,例如:
public static double fastPotRek(double x, int n) {
return aux(x,n,0);
}
private static double aux(double x, int n, int c) {
...
}
我有一个递归的方法,用某种算法计算x^n,但这在这里并不重要。重要的是我的辅助函数,它跟踪该算法的递归调用。
public class FastPot {
public static double fastPotRek(double x, int n) {
class Aux {
private double aux(double x, int n, int c) {
if (n == 0) {System.out.print("It took "+c+" recursive calls to compute "); return 1;}
else return (n % 2 == 0) ? aux(x*x, n/2, c+1)
: x * aux(x, n-1, c+1);
}
}
Aux a = new Aux();
return a.aux(x, n, 0);
}
}
为了稍微组织一下,我想在 fastPotRek
内声明 aux
,为此您必须使用内部 class,我不能将其方法声明为静态的。因此,我实例化 Aux a = new Aux();
以便能够调用 aux
.
请告诉我有一种方法可以使它更优雅,并告诉我我忽略了什么......就像蜜蜂能够以某种方式使 aux
静态或不需要实例化 Aux
。
不需要内部 class 也不需要使其成为静态的:
public class FastPot {
//Static, use only from within FastPot
private static double aux(double x, int n, int c) {
if (n == 0) {
System.out.print("It took "+c+" recursive calls to compute ");
return 1;
} else {
return (n % 2 == 0) ? aux(x*x, n/2, c+1)
: x * aux(x, n-1, c+1);
}
}
}
//Your outward interface
public static double fastPotRek(double x, int n) {
return aux(x, n, 0);
}
}
或者如果你坚持使用内部 class:
public class FastPot {
//Static, use only from within FastPot
private static class Aux {
private static double aux(double x, int n, int c) {
if (n == 0) {
System.out.print("It took "+c+" recursive calls to compute ");
return 1;
} else {
return (n % 2 == 0) ? aux(x*x, n/2, c+1)
: x * aux(x, n-1, c+1);
}
}
}
//Your outward interface
public static double fastPotRek(double x, int n) {
return Aux.aux(x, n, 0);
}
}
你有这个:
Aux a = new Aux();
return a.aux(x, n, 0);
我会这样写(一样):
return new Aux().aux(x, n, 0);
编辑: 我用完了 Whosebug。你们不需要帮助吗?我不会给它。以后我只问我的个问题,不回答
我会 post 这个答案,尽管很多人(包括我)对此并不满意。请不要使用这种代码:
public static double fastPotRek(double x, int n) {
return new Cloneable() {
private double aux(double x, int n, int c) {
if (n == 0) {System.out.print("It took "+c+" recursive calls to compute "); return 1;}
else return (n % 2 == 0) ? aux(x*x, n/2, c+1) : x * aux(x, n-1, c+1);
}
}.aux(x, n, 0);
}
同样,我强烈建议使用私有静态方法,例如:
public static double fastPotRek(double x, int n) {
return aux(x,n,0);
}
private static double aux(double x, int n, int c) {
...
}