在java类的main方法中传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4247240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing arguments in main method in java class
提问by Rizwan
Can someone tell me what is the need to declare a class like this:
有人可以告诉我需要声明这样的类吗:
public class Test {
String k;
public Test(String a, String b, String c){
k = a + " " + b + " " + c; //do something
}
public void run(){
System.out.println(k);
}
public static void main(String[] args) {
String l = args[0];
String m = args[1];
String n = args[2];
Test obj = new Test(l,m,n);
obj.run();
}
}
Of course it works but I don't get the point why would one use such way to implement something. Is it because we need to pass arguments directly to the class main method that is why we use this way or is there some other reason?
当然它有效,但我不明白为什么要使用这种方式来实现某些东西。是因为我们需要将参数直接传递给类 main 方法,这就是我们使用这种方式的原因还是有其他原因?
What is the purpose of public Test(...)
using the same class name. Why is it like this?
public Test(...)
使用相同的类名的目的是什么。为什么会这样?
采纳答案by Buhake Sindi
The public Test(...)
is a constructorand its purpose is for object creation. This is clearly seen from the sample code...
该public Test(...)
是一个构造函数,其目的是创建对象。从示例代码中可以清楚地看到这一点......
Test obj = new Test(l,m,n);
The variable obj
is instantiated with object Test
by being assigned to the Test
's constructor. In java, every constructor musthave the exactsame name (and case) as the java file it's written in (In your case constructor Test
is found in Test.java).
该变量通过被分配给的构造函数来obj
使用 objectTest
进行实例化Test
。在 java 中,每个构造函数都必须与编写它的 java 文件具有完全相同的名称(和大小写)(在您的情况下,构造函数Test
位于 Test.java 中)。
...Why is it like this?
……为什么会这样?
It all depends on what you want to do with your object. You could have a zero-argument constructor (i.e. requires no parameters) and have methods to set your l
, m
, n
, like so:
这一切都取决于你想用你的对象做什么。你可以有一个零参数构造函数(即不需要参数)并有方法来设置你的l
, m
, n
,如下所示:
package net;
public class Test {
private String k;
/**
*
*/
public Test() {
super();
// TODO Auto-generated constructor stub
}
public void set(String a, String b, String c) {
k = a + " " + b + " " + c; //do something
}
public void run() {
System.out.println(k);
}
public static void main(String[] args) {
String l = args[0];
String m = args[1];
String n = args[2];
Test obj = new Test();
obj.set(l, m, n);
obj.run();
}
}
As you can see, it's exactly the same feature as your example but with a zero-argument constructor.
如您所见,它与您的示例功能完全相同,但具有零参数构造函数。
If your class has no constructor at all, java adds a public zero-argument constructor for you automatically.
如果您的类根本没有构造函数,java 会自动为您添加一个公共零参数构造函数。
Hope this helps.
希望这可以帮助。
回答by Klaus Byskov Pedersen
The method called Test
is a so-called constructorfor the Test
class. The constructor is the method that gets called when you write something like new Test(...)
.
调用的方法Test
是所谓的构造函数的Test
类。构造函数是在您编写类似new Test(...)
.
Bear in mind that the main
method is a static
method, which means that it does not require an instance of the class Test to be called. This is not the case for the run
method. run
is an instance method, and to invoke it you need an instance of the Test
class (the obj
in your case).
请记住,该main
方法是一个static
方法,这意味着它不需要调用 Test 类的实例。方法不是这种情况run
。run
是一个实例方法,要调用它,您需要一个Test
类的实例(obj
在您的情况下)。
回答by Powerlord
The main
method is the entry point for the program and is called when you run java Test
from the command line.
该main
方法是程序的入口点,在您从命令行运行时会被调用java Test
。
public Test(String a, String b, String c)
is a public
constructorfor the Test
class and is called when you call new Test(l,m,n);
Note that a
in the constructor and l
in main method refer to the same String
... this also applies to b
and m
; c
and n
.
public Test(String a, String b, String c)
是该类的public
构造函数,在Test
调用时会被调用new Test(l,m,n);
注意,a
在构造函数中和l
main 方法中指的是相同的String
……这也适用于b
和m
;c
和n
。
As a side note, this class expects to be passed three values from the command line, and then stores them in l
, m
, and n
作为一个侧面说明,这个类预计将在命令行中传递三个值,然后将它们存储l
,m
和n
One last note: If you have a method with the signature public void run()
, your class should likely implementRunnable
so that it can be used in a Thread
.
最后一个注意事项:如果您有一个带有签名的方法public void run()
,您的类可能应该实现Runnable
以便它可以在Thread
.
回答by brain
The public Test(...) bit is the constructor of that class. It always has the same name as the class. Classes and main methods are two quite different aspects of programming. Classes define reusable components that have both state and methods. The main method is a special method that gets called from the command line.
公共 Test(...) 位是该类的构造函数。它始终与类同名。类和主要方法是编程的两个完全不同的方面。类定义了具有状态和方法的可重用组件。main 方法是一种从命令行调用的特殊方法。
Your example is so trivial that it doesnt really show any benefits of Object Orientated Programming. If you consider an example where you had different Classes intetracting you might get more of a feel for it.
你的例子太琐碎了,它并没有真正显示出面向对象编程的任何好处。如果您考虑一个示例,其中您有不同的类相互关联,您可能会对它有更多的感觉。
回答by Erick Robertson
Learn Java.
学习Java。
A constructor is a function that gets called to create an object, and it's denoted by a function with the same name as the class, but no return type. Multiple constructors can be declared with different arguments.
构造函数是一个被调用以创建对象的函数,它由一个与类同名的函数表示,但没有返回类型。可以使用不同的参数声明多个构造函数。
In this case, the arguments are taken out of the argument array and passed as arguments to the constructor for Test
.
在这种情况下,参数从参数数组中取出并作为参数传递给 的构造函数Test
。
These are fundamentally basic concepts to the Java programming language. You should read up on Java. Try Thinking in Java, this is a great book to get started.
这些是 Java 编程语言的基本概念。你应该阅读Java。尝试用 Java 思考,这是一本很好的入门书。