简体   繁体   English

纠正Java中的主要方法,并理解其含义

[英]Correct main method in Java, and understanding their meanings

I've got a quick question that probably borders on dumb (to you Java experts out there), but I've been having some trouble understanding the basic "main method" syntax and why it seems to vary. 我有一个快速的问题,可能是愚蠢的(对您来说Java专家在那里),但是我在理解基本的“主要方法”语法以及为什么它看起来有所变化时遇到了一些麻烦。

First a little background: I've been taking some basic Java courses, and I've been able to pick it up fairly quickly because I've done some programming in other languages (VBA, Python, etc). 首先介绍一些背景知识:我已经学习了一些基本的Java课程,并且由于能够用其他语言(VBA,Python等)进行过一些编程,因此能够很快上手。

But anyway, for the "main method", this course uses: 但是无论如何,对于“主要方法”,本课程使用:

public void run() {
    // do whatever we were discussing
}

However, the compilers I've been using (www.compileonline.com, and now most recently JGrasp) apparently only recognize this: 但是,我一直在使用的编译器(www.compileonline.com,现在是最近的JGrasp)显然只能识别以下内容:

public static void main(String args[]) {
    // code goes here.
}

Now I have a basic understanding of this (what "public", "void" and even "static" mean) but I'm not sure I understand why the course lessons use run() and the compilers say otherwise. 现在,我对此有了基本的了解(“ public”,“ void”甚至“ static”是什么意思),但是我不确定我为什么理解本课程为什么要使用run(),而编译器却不这么说。 I find it interesting that the compilers require the "main" method to be static (like a constant or class variable) and to use a string called args; 我发现有趣的是,编译器要求“ main”方法是静态的(如常量或类变量)并使用称为args的字符串。 I've never used this string, but for whatever reason it seems to be needed. 我从未使用过此字符串,但是无论出于什么原因,似乎都需要它。

Similarly, the courses I've been learning from use a simple "println" but the compilers need "System.out.println"... just thought I should mention that because I'm thinking it's part of the same situation. 同样,我一直在学习的课程都使用简单的“ println”,但是编译器需要“ System.out.println”……我只是想提一提,因为我认为这是同一情况的一部分。

Anyway, does anyone know off-hand what's going on here? 无论如何,有人知道这是怎么回事吗? It's no big deal, because it's easy to just copy/paste the compilers' required text into the courses' code to get similar results, but it would be great to actually know why this is the case (as I'm sure this sort of copy/paste strategy will lead to "bugs" in the future). 没什么大不了的,因为只需要简单地将编译器所需的文本复制/粘贴到课程代码中即可获得相似的结果,但是实际上知道为什么会这样很重要(因为我敢肯定这种情况复制/粘贴策略在将来会导致“错误”。 The only thing that I can come up with off-hand is maybe there was an update to Java that drastically changed certain functions and is not backwards-compatible? 我唯一能想到的就是也许Java的更新彻底改变了某些功能并且不向后兼容? Anyway, thanks in advance for your input. 无论如何,在此先感谢您的输入。 : ) :)

Here's a basic Java class. 这是一个基本的Java类。

public class Basic implements Runnable {

    @Override
    public void run() {
        // TODO Code goes here
    }

    public static void main(String[] args) {
        Basic basic = new Basic();
        basic.run();
    }

} 

This is how a main method and a run method work together. 这是main方法和run方法一起工作的方式。 You put your code in the run method, and you execute the code from the main method. 您将代码放入run方法,然后从main方法执行代码。

The main method is static because Java has to have a single entry point into your classes and methods. 主要方法是静态的,因为Java必须在您的类和方法中有一个入口点。 The args String array allows you to pass strings to your program from the command line where you execute your program. args String数组允许您从执行程序的命令行将字符串传递给程序。

As an example, a file copy program would have two args parameters, the input file path and the output file path. 例如,文件复制程序将具有两个args参数,即输入文件路径和输出文件路径。 This allows the file copy program to copy any file. 这允许文件复制程序复制任何文件。

The real main method in Java for stand-alone applications is Java对于独立应用程序的真正主要方法是

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

Everything else is part of a framework that has its own main method that sets up some things for you and then calls your code. 其他所有内容都是框架的一部分,该框架具有自己的main方法,该方法可以为您设置一些东西,然后调用您的代码。

How it calls your code depends on the framework. 它如何调用您的代码取决于框架。 If it's a web framework, it might call your doGet and doPost methods (servlet), or the methods annotated with @GET and @POST in a class annotated with @Path (JAX-RS). 如果是Web框架,则它可能会调用doGet和doPost方法(servlet),或在@Path注释的类(JAX-RS)中用@GET和@POST注释的方法。 If it's an applet or a JavaFX application, your code would be called in a different way and so on. 如果是applet或JavaFX应用程序,则将以其他方式调用您的代码,依此类推。

The run method implements the Runnable interface for executing threads. run方法实现用于执行线程的Runnable接口。

http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

I would suspect that the courses you're taking are deliberately avoiding the normal, more verbose syntax to make the content more accessible to students without prior experience in programming. 我怀疑您所修的课程是故意避免使用常规的,更冗长的语法,以使没有先前编程经验的学生可以更轻松地访问内容。

For a beginner, things like public static void main(String[] args) and System.out.println() can be quite a mouthful for the seemingly simple ideas they represent. 对于初学者来说,诸如public static void main(String[] args)System.out.println()对于它们所代表的看似简单的想法来说可能是一大口。 As a result, some instructors choose to avoid "real" Java until their students are ready to grasp concepts like arrays and static fields/methods. 结果,一些教师选择避免使用“真正的” Java,直到他们的学生准备好掌握数组和静态字段/方法之类的概念为止。

This pedagogical approach also helps to keep students from running into errors they don't yet understand, such as calling non-static methods in a static context. 这种教学方法还有助于防止学生遇到他们尚未理解的错误,例如在静态上下文中调用非静态方法。 As others have mentioned, it also makes it easy to ensure students write code that is compatible with a given testing framework. 正如其他人所提到的,这也使确保学生编写与给定测试框架兼容的代码变得容易。

As for whether it's a good or bad approach, I can't really say. 至于这是好方法还是坏方法,我不能说真的。 I'm sure some students appreciate the softer introduction, while others may feel that the "hand-holding" is an unnecessary hindrance. 我敢肯定,有些学生会喜欢这种较柔和的介绍,而另一些学生可能会觉得“把手”是不必要的障碍。

Of course, if you intend to write real-world Java programs, you'll need to familiarize yourself with the way Java really works at some point, which involves the verbose forms you've pointed out. 当然,如果您打算编写实际的 Java程序,则需要在某种程度上熟悉Java的实际工作方式,这涉及您指出的冗长形式。

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

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