简体   繁体   中英

I can't pass the argument while using higher order function

Here you can see that I can't pass that string parameter

I somehow learned how to pass a method in the same class but, when I want to call it from another class, I can't pass the arguments.

Several issues here:

  1. the introduceMyself method accepts a higher-order function, but in its usage it seems that you don't want the method itself, but its result. I'd suggest replacing the getName parameter with name parameter and convert it to a regular string which will be provided externally by an invocation of said method.
  2. In line 6, you have a syntax error. Since you're using an instance - person , you can replace the :: with a . like so: person.myNameIs("michael") . It's not working because :: passes the method itself, which you don't really want. Again, your code implies that you need to pass the result of the method and not the method itself.

Finally, you can pass higher order functions, but you can't pass them "with an argument". You can do one of 3 things:

  1. Invoke the HOF locally and pass the result of the invocation
  2. Pass the HOF and the argument and invoke them wherever it is you need to.
  3. Pass the HOF and let the target pass whatever argument it chooses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM