Why do we need to specify parameter name in interface?

拟墨画扇 提交于 2019-12-03 08:27:25

问题


When we create interface methods, can't we do something like in java:

void interface_method(Integer,String, /* other parameter */);

Instead I noticed that we also need to give the parameter names lile:

void interface_method(Integer i, String s);

Also the interface implementor don't need to have the same parameter name as in interface method.

I found a similar question about c# over here . They mention one scenario of named parameter but I don't find any other relevent reason specifically in java.


回答1:


From a technical standpoint it wouldn't be necessary.

I've always taken it as a syntax normalization, and a documentation aid.

This way there's:

  1. No reason to differentiate between class and interface method syntax, and
  2. Default Javadoc documentation can be generated using the (descriptive!) parameter name.



回答2:


Without parameter names it becomes very hard to distinguish between parameters of the same type.

double divide(double, double);

Convention says that the second parameter would be the divisor. But with named parameters, it is explicit and much clearer. In turn, the documentation can clearly use the names instead having to continually say "first parameter" or "second parameter" when trying to explain how the method should be use and what each parameter is meant to represent.




回答3:


My take on this:

  1. This is in language specification for parameters to have names;

  2. Interface being interface, names help to communicate the meaning;

  3. Interface is not really a forward declaration of implementation that will follow like in C/C++, so comparison is not entirely correct;



来源:https://stackoverflow.com/questions/10998151/why-do-we-need-to-specify-parameter-name-in-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!