简体   繁体   English

制作除字符串以外的主要接受对象?

[英]Making main accept object other than string?

Is it possible to have the main method accept an argument other than a string array? 是否可以让main方法接受除字符串数组之外的参数? For example, can we have a method like main(Animals[]args){/*code*/} ? 例如,我们可以有一个像main(Animals[]args){/*code*/}吗? If not, then why? 如果没有,那么为什么呢?

No - the entry point is always the method with the header public static void main(String[] args) (ie the JVM invokes this particular method). 否 - 入口点始终是标头public static void main(String[] args) (即JVM调用此特定方法)。 You can certainly define a method 你当然可以定义一个方法

public static void main(Animals[] args) {...}

but it would not be executed directly upon running the program. 但是在运行程序时不会直接执行。


EDIT : The reason the main method specifically has a string-array argument is because this array will contain the command-line arguments when the program is run. 编辑main方法特别具有字符串数组参数的原因是因为该数组将在程序运行时包含命令行参数 Intuitively, these should be strings (and certainly not Animal s, for example). 直观地说,这些应该是字符串(例如,当然不是Animal s)。

Because when you input from the command line you are entering strings, not a complex type. 因为从命令行输入时输入的是字符串,而不是复杂类型。

So it makes sense for the argument to be an array of pointers (references) to strings, including or not (depending on the language) the count of the arguments. 因此,参数是一个指向字符串的指针(引用)数组是有意义的,包括或不包括(取决于语言)参数的计数。 In Java this isn't needed as you can just use length . 在Java中,这不是必需的,因为您可以使用length

Not sure why you'd want that?.. 不知道你为什么要这样?

规范说它只能处理字符串数组。

There is nothing stopping you from having a public static void main that takes a parameter type other than String. 没有什么能阻止你使用一个带有String以外的参数类型的public static void main。 The problem is the that the JVM needs simple rules for identifying and calling the method. 问题是JVM需要简单的规则来识别和调用方法。 It has an array of strings, eg from the command line, available to pass to the program. 它有一个字符串数组,例如来自命令行,可以传递给程序。 How would the JVM go about turning it into an array of Animal, or some other type, before it has started running your program? 在开始运行程序之前,JVM如何将其转换为Animal或其他类型的数组?

Here's an example of a main that takes a different parameter type, and of the program itself dealing with producing the Animal array from the array of strings that the JVM has. 下面是一个main的示例,它采用不同的参数类型,以及程序本身处理从JVM具有的字符串数组生成Animal数组的示例。 Of course, it would really be better to give the second main method a more meaningful name. 当然,给第二个主要方法提供一个更有意义的名称真的会更好。

  import java.util.Arrays;

  public class Bad {

    public static void main(String[] args) {
      Animal[] animals = new Animal[args.length];
      for (int i = 0; i < args.length; i++) {
        animals[i] = new Animal(args[i]);
      }
      main(animals);
    }

    public static void main(Animal[] args) {
      System.out.println(Arrays.asList(args));
    }

  }

  class Animal {
    String species;

    public Animal(String species) {
      this.species = species;
    }

    public String toString() {
      return "Animal: " + species;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM