Java 如何设置默认方法参数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038383/
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
how to set default method argument values?
提问by Niko Gamulin
Is it possible to set the default method parameter values in Java?
是否可以在 Java 中设置默认方法参数值?
Example: If there is a method
例子:如果有一个方法
public int doSomething(int arg1, int arg2)
{
//some logic here
return 0;
}
is it possible to modify the given method in order to be able to call it with and without parameters?
是否可以修改给定的方法,以便能够在带参数和不带参数的情况下调用它?
example:
例子:
doSomething(param1, param2);
doSomething();
Thanks!
谢谢!
回答by kgiannakakis
No. Java doesn't support default parameters like C++. You need to define a different method:
不。Java 不支持像 C++ 这样的默认参数。您需要定义一个不同的方法:
public int doSomething()
{
return doSomething(value1, value2);
}
回答by Andrew Hare
You can accomplish this via method overloading.
您可以通过方法重载来实现这一点。
public int doSomething(int arg1, int arg2)
{
return 0;
}
public int doSomething()
{
return doSomething(defaultValue0, defaultValue1);
}
By creating this parameterless method you are allowing the user to call the parameterfull method with the default arguments you supply within the implementation of the parameterless method. This is known as overloadingthe method.
通过创建此无参数方法,您允许用户使用您在无参数方法的实现中提供的默认参数调用有参数的方法。这称为方法重载。
回答by matt b
You can't declare default values for the parameters like C# (I believe) lets you, but you could simply just create an overload.
您不能像 C#(我相信)那样为参数声明默认值,但您可以简单地创建一个重载。
public int doSomething(int arg1, int arg2) {
//some logic here
return 0;
}
//overload supplies default values of 1 and 2
public int doSomething() {
return doSomething(1, 2);
}
If you are going to do something like this please do everyone else who works with your code a favor and make sure you mention in Javadoc comments what the default values you are using are!
如果你打算做这样的事情,请帮助所有使用你的代码的人,并确保你在 Javadoc 注释中提到你使用的默认值是什么!
回答by Savvas Dalkitsis
You can overload the method with different parameters:
您可以使用不同的参数重载该方法:
public int doSomething(int arg1, int arg2)
{
//some logic here
return 0;
}
public int doSomething(
{
doSomething(0,0)
}
回答by akarnokd
If your arguments are the same type you could use varargs:
如果您的参数类型相同,则可以使用 varargs:
public int something(int... args) {
int a = 0;
int b = 0;
if (args.length > 0) {
a = args[0];
}
if (args.length > 1) {
b = args[1];
}
return a + b
}
but this way you lose the semantics of the individual arguments, or
但是这样你就失去了各个参数的语义,或者
have a method overloaded which relays the call to the parametered version
有一个重载的方法,它将调用中继到参数化版本
public int something() {
return something(1, 2);
}
or if the method is part of some kind of initialization procedure, you could use the builder pattern instead:
或者如果该方法是某种初始化过程的一部分,您可以改用构建器模式:
class FoodBuilder {
int saltAmount;
int meatAmount;
FoodBuilder setSaltAmount(int saltAmount) {
this.saltAmount = saltAmount;
return this;
}
FoodBuilder setMeatAmount(int meatAmount) {
this.meatAmount = meatAmount;
return this;
}
Food build() {
return new Food(saltAmount, meatAmount);
}
}
Food f = new FoodBuilder().setSaltAmount(10).build();
Food f2 = new FoodBuilder().setSaltAmount(10).setMeatAmount(5).build();
Then work with the Food object
然后使用 Food 对象
int doSomething(Food f) {
return f.getSaltAmount() + f.getMeatAmount();
}
The builder pattern allows you to add/remove parameters later on and you don't need to create new overloaded methods for them.
构建器模式允许您稍后添加/删除参数,并且您不需要为它们创建新的重载方法。
回答by Nick Holt
It's not possible to default values in Java. My preferred way to deal with this is to overloadthe method so you might have something like:
在 Java 中不可能有默认值。我处理这个问题的首选方法是重载该方法,因此您可能会遇到以下情况:
public class MyClass
{
public int doSomething(int arg1, int arg2)
{
...
}
public int doSomething()
{
return doSomething(<default value>, <default value>);
}
}
回答by djangofan
Hopefully I am not misunderstanding this document, but if your using Java 1.8, in theory, you could accomplish something like this by defining a working implementation of a default method ("defender methods") within an interface that you implement.
希望我没有误解本文档,但如果您使用 Java 1.8,理论上,您可以通过在您实现的接口中定义默认方法(“防御者方法”)的工作实现来完成类似的操作。
interface DoInterface {
void doNothing();
public default void remove() {
throw new UnsupportedOperationException("remove");
}
public default int doSomething( int arg1, int arg2) {
val = arg1 + arg2 * arg1;
log( "Value is: " + val );
return val;
}
}
class DoIt extends DoInterface {
DoIt() {
log("Called DoIt constructor.");
}
public int doSomething() {
int val = doSomething( 0, 100 );
return val;
}
}
Then, call it either way:
然后,以任何一种方式调用它:
DoIt d = new DoIt();
d.doSomething();
d.soSomething( 5, 45 );