问题描述:

for example with C++ I can write

void func(int a=1,int b=2){...}

using Java I have to create several methods

网友答案:

It wasn't a feature that the creators of Java thought was important enough to include. Remember that Java was partially created as a reaction against the complexities of C++.

网友答案:

Simple. That's not part of Java language specifications. It wasn't included in Java language design. As Andrew stated here, one of the motivations that lead Java to its current state was the reaction against the complexities of C++. Besides, I think this isn't a feature you will find in any true OO language.

There's an workaround, thought. You can use a variable length argument list, such as:

void func(int... a) { ... }

This way, you can use a as an array of integers, and check for default values inside one method. Note that this doesn't follow any design pattern, and isn't a good practice. It's only an workaround for the feature you desire.

相关阅读:
Top