在主方法内写入一个函数--Java [英] Writing a function inside the main method - Java

查看:96
本文介绍了在主方法内写入一个函数--Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以在主要方法里写一个方法吗?例如,我发现这个代码:

  public class TestMax {
public static void main(String [] args){
int i = 5;
int j = 2;
int k = max(i,j);
System.out.println(之间的最大值是+ k);


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

返回结果;
}
}

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

解决方案

当Java 8发布时,Closure / Lambda功能应该使它可以在主方法。在此之前,您只能在特殊情况下在主要方法中定义方法。

碰巧,你的问题确实属于特殊情况。有一个接口(Comparable)封装了比较两种相同类型事物的逻辑。结果,代码可以被重写为:

  public class TestMax {
public static void main(String [] args){
int i = 5;
int j = 2;
比较器<整数> compare = new Comparator< Integer>(){
@Override
public int compare(Integer o1,Integer o2){
//因为Integer已经实现了Comparable方法,
/ /这可以改为return o1.compareTo(o2);
return o1 - o2;
}
};
//请注意,这会将您的整数自动装箱到整数。
int k = compare.compare(i,j)> 0?我:j;
System.out.println(之间的最大值是+ k);






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

pre $ public class TestMax {
public static void main(String [] args){
int i = 5;
int j = 2;
//'自然'排序意味着使用在整数上定义的compareTo方法。
int k = Ordering。< Integer> natural()。max(i,j);
System.out.println(之间的最大值是+ k);
}
}

我怀疑你的问题更多的是关于Java语言而不是标准实践与订购数字(和其他事物)有关。所以这可能没有用,但我想我会分享一下。


Can you write a method inside the main method? For example I found this code:

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

Can the method max be coded inside the main method?

解决方案

When Java 8 comes out, the Closure/Lambda functionality should make it so that you can define the max method in the main method. Until then, you'll only be able to define a method in the main method in special circumstances.

As it happens, your question does fall into the special circumstance. There is an interface (Comparable) which encapsulates the logic of comparing two things of the same type. As a result, the code could be rewritten as follows:

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

This only works because the comparator interface already exists in the standard Java distribution. The code could be made better through the use of libraries. If I were writing this code, I would add Google Guava to my classpath. Then I could write the following:

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

I suspect that your question was more about the abilities of the Java language rather than standard practices having to do with ordering numbers (and other things). So this might not be useful, but I thought I'd share just in case.

这篇关于在主方法内写入一个函数--Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆