Why do we need to specify parameter name in interface?

前端 未结 3 1863
离开以前
离开以前 2021-02-04 00:45

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

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


        
相关标签:
3条回答
  • 2021-02-04 01:17

    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.

    0 讨论(0)
  • 2021-02-04 01:21

    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.
    0 讨论(0)
  • 2021-02-04 01:23

    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;

    0 讨论(0)
提交回复
热议问题