Java 更优雅的内部方法声明(没有所有的混乱)?

Java 更优雅的内部方法声明(没有所有的混乱)?,java,syntax,inner-classes,Java,Syntax,Inner Classes,我有一个递归方法,用某种算法计算x^n,但这在这里并不重要。重要的是我的helper函数,它跟踪这个算法的递归调用 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.

我有一个递归方法,用某种算法计算x^n,但这在这里并不重要。重要的是我的helper函数,它跟踪这个算法的递归调用

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,为此您必须使用一个内部类,我不能声明其静态方法。因此,我实例化了Aux a=新的Aux;能够呼叫辅助


请告诉我有一种方法可以让这个更优雅,让我看看我忽略了什么。。。就像Being能够以某种方式使aux成为静态的,或者不需要实例化aux。

不需要内部类,也不需要使该静态:

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);
    }
}
或者如果您坚持使用内部类:

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);
    }
}

不需要内部类,也不需要使其成为静态的:

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);
    }
}
或者如果您坚持使用内部类:

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);
编辑:我已经完成了StackOverflow。你们不需要帮助吗?我不会给你的。从现在起,我只会问我的问题,不会回答。

你有这个:

Aux a = new Aux();
return a.aux(x, n, 0);
我会这样写,同样的:

return new Aux().aux(x, n, 0);

编辑:我已经完成了StackOverflow。你们不需要帮助吗?我不会给你的。从现在起,我只会问我的问题,不会回答。

我会发布这个答案,尽管包括我在内的很多人对此都不满意。请不要使用此类代码:

我再次强烈建议使用私有静态方法,如:

public static double fastPotRek(double x, int n) {
    return aux(x,n,0);
}
private static double aux(double x, int n, int c) {
    ...
}

虽然包括我在内的很多人都不喜欢这个答案,但我还是会发布这个答案。请不要使用此类代码:

我再次强烈建议使用私有静态方法,如:

public static double fastPotRek(double x, int n) {
    return aux(x,n,0);
}
private static double aux(double x, int n, int c) {
    ...
}

您可以将Aux设置为FastPot的静态内部类或使用lambdas。除此之外,为什么不使用普通的静态方法呢?如果你试图组织代码,但却以这种方式产生混乱,我不确定这种组织方式有多大意义。我真的看不出内部类的意义。将aux方法作为私有静态方法移动到与fastPotRek相同的级别将完成这项工作。我知道这看起来很糟糕,这就是为什么我问你是否可以在这种情况下嵌套而不会造成混乱:你可以使aux成为FastPot的静态内部类或使用lambdas。除此之外,为什么不使用普通的静态方法呢?如果你试图组织代码,但却以这种方式产生混乱,我不确定这种组织方式有多大意义。我真的看不出内部类的意义。将aux方法作为私有静态方法移动到与fastPotRek相同的级别将完成这项工作。我知道这看起来很糟糕,这就是为什么我问你是否可以在这种情况下嵌套,而不会造成混乱:这并不是真正的回答问题;这应该是评论而不是回答!请告诉我有一种方法可以让这个更优雅,让我看看我忽略了什么。。。比如说,being能够以某种方式使aux成为静态的,或者不需要实例化aux。我的代码没有实例化Aux:@Mayuso是的,它实例化了!有一个新的关键词,它并没有真正回答这个问题;这应该是评论而不是回答!请告诉我有一种方法可以让这个更优雅,让我看看我忽略了什么。。。比如说,being能够以某种方式使aux成为静态的,或者不需要实例化aux。我的代码没有实例化Aux:@Mayuso是的,它实例化了!这里有一个新的关键字