带参数的方法
Methods with parameters
在方法参数中声明变量和在方法内部声明变量有区别吗?两者都打印出相同的变量,但我想是有区别的。
通过将变量声明为方法参数,您可以将变量传递给方法
public void printIt(String text){
System.out.println(text);
}
但是如果你像这样在方法中声明变量:
public void printIt(){
String name;
//you can't pass
}
如果你指的是我认为的你,那么两者之间就有很大的区别。以此为例:
public void printText() {
System.out.println("Hello World");
}
这将打印出文本 Hello World
。现在看看这个方法:
printText("Hello World");
public void printText(String text) {
System.out.println(text);
}
这两个示例中的后者提供了更大的灵活性,因为您可以使用您喜欢的任何参数调用该方法,而前者每次只会打印 Hello World
。当然,根据您希望方法执行的操作,一种形式可能比另一种形式更合适,但方法参数允许重用。
来自Javadoc
Local Variables Similar to how an object stores its state in fields, a
method will often store its temporary state in local variables. The
syntax for declaring a local variable is similar to declaring a field
(for example, int count = 0;
). There is no special keyword designating
a variable as local; that determination comes entirely from the
location in which the variable is declared — which is between the
opening and closing braces of a method. As such, local variables are
only visible to the methods in which they are declared; they are not
accessible from the rest of the class.
Parameters ... Recall that the signature for the main method is public
static void main(String[] args)
. Here, the args variable is the
parameter to this method. The important thing to remember is that
parameters are always classified as "variables"
not "fields"
.
并且还通过在 Java 中的方法中定义参数(我想这同样适用于 C++),这会导致 方法重载 。
请查看下面提供的示例:
public void createPerson(int ID, String name) {
// do something here
}
public void createPerson(double height, String name) {
// do something here
}
public void createPerson(double height, String name, boolean testIt) {
// do something here
}
所以它的帮助基本上是改变方法的方法签名,这导致开发人员在 3 个方法中实现不同的方法体,但具有相同的方法名称但具有不同的方法签名。
从 Oracle Java.
中进一步阅读以下 link 中的方法
希望对您的问题有所帮助。
在方法参数中声明变量和在方法内部声明变量有区别吗?两者都打印出相同的变量,但我想是有区别的。
通过将变量声明为方法参数,您可以将变量传递给方法
public void printIt(String text){
System.out.println(text);
}
但是如果你像这样在方法中声明变量:
public void printIt(){
String name;
//you can't pass
}
如果你指的是我认为的你,那么两者之间就有很大的区别。以此为例:
public void printText() {
System.out.println("Hello World");
}
这将打印出文本 Hello World
。现在看看这个方法:
printText("Hello World");
public void printText(String text) {
System.out.println(text);
}
这两个示例中的后者提供了更大的灵活性,因为您可以使用您喜欢的任何参数调用该方法,而前者每次只会打印 Hello World
。当然,根据您希望方法执行的操作,一种形式可能比另一种形式更合适,但方法参数允许重用。
来自Javadoc
Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example,
int count = 0;
). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.Parameters ... Recall that the signature for the main method is public static void
main(String[] args)
. Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as"variables"
not"fields"
.
并且还通过在 Java 中的方法中定义参数(我想这同样适用于 C++),这会导致 方法重载 。
请查看下面提供的示例:
public void createPerson(int ID, String name) {
// do something here
}
public void createPerson(double height, String name) {
// do something here
}
public void createPerson(double height, String name, boolean testIt) {
// do something here
}
所以它的帮助基本上是改变方法的方法签名,这导致开发人员在 3 个方法中实现不同的方法体,但具有相同的方法名称但具有不同的方法签名。
从 Oracle Java.
中进一步阅读以下 link 中的方法希望对您的问题有所帮助。