【问题标题】:Writing a function inside the main method - Java在 main 方法中编写函数 - Java
【发布时间】:2014-01-20 05:04:29
【问题描述】:

你能在main方法里面写一个方法吗?例如我发现了这段代码:

public class TestMax {
    public static void main(String[] args) {
    int i = 5;
    int j = 2;
    int k = max(i, j);
    System.out.println("The maximum between is " + k);
}

 public static int max(int num1, int num2) {
    int result;
    if (num1 > num2)
       result = num1;
    else
       result = num2;

    return result; 
  }
}

方法 max 可以在 main 方法中编码吗?

【问题讨论】:

  • 您不能将方法直接嵌套在 any 方法中。

标签: java function methods main


【解决方案1】:

不,你不能在另一个方法中声明一个方法。

如果您仔细查看您提供的代码,这只是格式错误的一种情况,main 方法在 max 方法声明之前结束。

【讨论】:

  • 他可以从方法中获取代码并将其放入主方法中。
【解决方案2】:

当 Java 8 出现时,Closure/Lambda 功能应该可以实现,以便您可以在 main 方法中定义 max 方法。在那之前,您只能在特殊情况下在 main 方法中定义一个方法。

碰巧,您的问题确实属于特殊情况。有一个接口(Comparable),它封装了比较两个相同类型的事物的逻辑。因此,代码可以改写如下:

public class TestMax {
  public static void main(String[] args) {
    int i = 5;
    int j = 2;
    Comparator<Integer> compare = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            // Because Integer already implements the method Comparable,
            // This could be changed to "return o1.compareTo(o2);"
            return o1 - o2;
        }
    };
    // Note that this will autobox your ints to Integers.
    int k = compare.compare(i, j) > 0 ? i : j;
    System.out.println("The maximum between is " + k);
  }
}

这只是因为标准 Java 发行版中已经存在比较器接口。通过使用库可以使代码变得更好。如果我正在编写这段代码,我会将 Google Guava 添加到我的类路径中。然后我可以写如下:

public class TestMax {
  public static void main(String[] args) {
    int i = 5;
    int j = 2;
    // The 'natural' ordering means use the compareTo method that is defined on Integer.
    int k = Ordering.<Integer>natural().max(i, j);
    System.out.println("The maximum between is " + k);
  }
}

我怀疑您的问题更多是关于 Java 语言的能力,而不是与订购号(和其他事情)有关的标准做法。所以这可能没有用,但我想我会分享以防万一。

【讨论】:

  • 既然 java 8 出来了,我们不应该更新这个答案吗?
【解决方案3】:

如果您想使用它,在这种情况下,将其设为 static 并将其放置在类中但在 main 方法之外(就像您在代码中使用的那样)。然后您可以在main() 中调用它。

【讨论】:

    【解决方案4】:

    cannot directly define methods 在 Java 的其他方法中(也是 main 方法)。

    You should write the method in the same class as main. 
    

    注意:您可以在另一个方法中声明一个具有方法的类

    【讨论】:

      【解决方案5】:

      如果格式正确,您会注意到max 方法与main 方法出现在同一类中,但它不在main 方法中。

      【讨论】:

        【解决方案6】:

        是的,这可以通过 lembda 表达式实现: 为此,您至少应该使用 java 8

        import java.util.function.BiFunction;
        
        public class TestMax {
            public static void main(String[] args) {
                BiFunction<Integer, Integer, Integer> max = (a,b)-> a>b ? a : b;
        
                //now you can call method
        
                int i = 5;
                int j = 2;
                int k = max.apply(i, j);
                System.out.println("The maximum between is " + k);
            }
        }
        

        BiFunction 是一个函数式接口 java 编译器在内部将 lambda 表达式代码转换为 anonymouse 类,该类实现 BiFunction 接口并覆盖包含您的代码的 apply() 方法

        内部(编译器考虑)代码是:

        BiFunction<Integer, Integer, Integer> max = new BiFunction<Integer, Integer, Integer>(){
            public Integer apply(Integer a, Integer b){
                return a>b ? a : b; //your code
            }
        }
        

        【讨论】:

        • BiFunction 是一个函数式接口,java 编译器在内部将它转换成一个匿名函数,覆盖 public RetType apply(Arg1Type arg1, Arg2Type arg2){
        【解决方案7】:

        没有。不能直接在 java 中的方法中定义方法。特殊情况除外,例如内置类中预先声明的方法。 最好的方法是在同一个类中定义方法,当然调用它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-29
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-26
          • 2012-09-28
          相关资源
          最近更新 更多