Android: How to set a default value for an argument variable

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!


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


你可以像这样滥用重载:

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

没有必要超载任何东西,只需写:

public int getScore(int score, Integer... bonus)
{
    if(bonus.length > 0)
    {
        return score + bonus[0];
    }
    else
    {
        return score;
    }
}
链接地址: http://www.djcxy.com/p/20872.html

上一篇: 使用ArrayList作为构造函数的参数

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