Android:如何为参数变量设置默认值

发布于 2024-10-31 00:09:36 字数 257 浏览 0 评论 0原文

Android函数

PHP示例:

function HaHa($a = "Test")
{
    print $a; 
}

问题是如何在android中做到这一点...

public void someFunction(int ttt = 5)
{
   // something
}

上面的解决方案不起作用,我该怎么做?

谢谢!

Android function

PHP example:

function HaHa($a = "Test")
{
    print $a; 
}

The question is how to do it in android...

public void someFunction(int ttt = 5)
{
   // something
}

The solution above doesn't work, how can I do it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

反话 2024-11-07 00:09:36

不,Java 不支持函数参数的默认值。这里有一篇关于借用语言功能的有趣文章: http://java.dzone.com /news/default-argument-values-java

No, Java does not support default values for function parameteres. There's an interesting post about borrowing language features here: http://java.dzone.com/news/default-argument-values-java

倾城月光淡如水﹏ 2024-11-07 00:09:36

不需要重载任何东西,只需编写:

public int getScore(int score, Integer... bonus)
{
    if(bonus.length > 0)
    {
        return score + bonus[0];
    }
    else
    {
        return score;
    }
}

No need to overload anything, just write:

public int getScore(int score, Integer... bonus)
{
    if(bonus.length > 0)
    {
        return score + bonus[0];
    }
    else
    {
        return score;
    }
}
往日情怀 2024-11-07 00:09:36

你可以像这样滥用重载:

int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }

You can abuse overloading like this:

int someMethod() { return someMethod(42); }
int someMethod(int arg) { .... }
蓝礼 2024-11-07 00:09:36

您可以使用 3 点 语法:

public void doSomething(boolean... arg1) {
    boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}

you can use 3 dots syntax:

public void doSomething(boolean... arg1) {
    boolean MyArg1= (arg1.length >= 1) ? arg1 : false;
}
客…行舟 2024-11-07 00:09:36

您可以使用重载函数。
重载也可以,但如果您需要多个参数的默认值,您最终将创建许多具有所有可能的默认参数组合的方法,例如您使用的示例,假设您希望为 3 个参数设置默认值。你最终会得到这个

public void methodA(A arg1) {  }
public void methodA( B arg2,) {  }
public void methodA(C arg3) {  }
public void methodA(A arg1, B arg2) {  }
public void methodA(A arg1, C arg3) {  }
public void methodA( B arg2, C arg3) {  }
public void methodA(A arg1, B arg2, C arg3) {  }

所以这是我为我做的黑客,你也可以使用

public static void main(String[] args)
{
    defaultParameter();
    defaultParameter(true);
}

public static void defaultParameter(Boolean ...gender)
{
    boolean genderBoolean  = false; // It the default value you want to give
    if(gender.length == 1)
    {
        genderBoolean = gender[0];  // Overrided Value
    }
    System.out.println(genderBoolean);
}

上面的代码将生成结果

false
true

我在这里找到示例 java-default-parameter-values

You can use overloading a function .
The overloading is also okay but in case you need default values for multiple arguments you will end up creating having many methods with all possible combination of default arguments, for the example you use imagine you want to have a default value for the 3 arguments. you will end up with this

public void methodA(A arg1) {  }
public void methodA( B arg2,) {  }
public void methodA(C arg3) {  }
public void methodA(A arg1, B arg2) {  }
public void methodA(A arg1, C arg3) {  }
public void methodA( B arg2, C arg3) {  }
public void methodA(A arg1, B arg2, C arg3) {  }

So here's the hack i did for me , you can also use

public static void main(String[] args)
{
    defaultParameter();
    defaultParameter(true);
}

public static void defaultParameter(Boolean ...gender)
{
    boolean genderBoolean  = false; // It the default value you want to give
    if(gender.length == 1)
    {
        genderBoolean = gender[0];  // Overrided Value
    }
    System.out.println(genderBoolean);
}

The above code will genrate result

false
true

I find out here the example java-default-parameter-values

A君 2024-11-07 00:09:36

您现在可以使用 Kotlin 在 Android 中完成此操作!
Kotlin 支持默认参数,因此您可以编写一个像这样的函数:

fun addTheBook(title: String, author : String = "No Name"){...}

然后你可以像这样调用它:

addTheBook("The Road Less Traveled")

You can do it now in Android by using Kotlin!
Kotlin supports default arguments, so you can write a function like:

fun addTheBook(title: String, author : String = "No Name"){...}

And then you can call it just like:

addTheBook("The Road Less Traveled")
错爱 2024-11-07 00:09:36

Java 不支持可以执行您想要的操作的语法。

也许在 someFunction(int) 的开头,您可以只检查传入的值,如果您不喜欢传入的值,则分配一个不同的值。

if (ttt == 0) ttt = 5;

请注意,这个问题似乎与 android 无关,因此不正确标记。

Java doesn't support a syntax that will do what you want.

Perhaps at the start of someFunction(int), you could just check the incoming value, and assign a different value if you don't like what's coming in.

if (ttt == 0) ttt = 5;

Please note this question appears to have nothing to do with android, and so is improperly tagged.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文