简体   繁体   English

使用高阶函数时无法传递参数

[英]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. introduceMyself方法接受一个高阶函数,但在它的使用中,您似乎不想要方法本身,而是它的结果。 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.我建议将getName参数替换为name参数并将其转换为常规字符串,该字符串将通过调用所述方法在外部提供。
  2. In line 6, you have a syntax error.在第 6 行,您有一个语法错误。 Since you're using an instance - person , you can replace the :: with a .由于您使用的是实例person ,因此您可以将 :: 替换为 . like so: person.myNameIs("michael") .像这样: 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在本地调用 HOF 并传递调用结果
  2. Pass the HOF and the argument and invoke them wherever it is you need to.传递 HOF 和参数,并在需要的任何地方调用它们。
  3. Pass the HOF and let the target pass whatever argument it chooses.传递 HOF 并让目标传递它选择的任何参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当参数具有默认值时,为什么我不能使用导航组件将参数传递给片段? - Why can't I pass an argument to fragment using navigation component when that argument has a default value? 获取高阶函数中函数参数的名称 - Get name of function argument in higher-order function Kotlin高阶函数传递一个带有可变数量参数的函数作为参数 - Kotlin Higher Order Function pass a function with variable number of arguments as the parameter 为什么C函数不能将正确的参数传递给Java? - Why can't C function pass right argument to java? BindingAdapter中的高阶函数 - Higher order function in BindingAdapter ClickableSpan 的高阶 function - Higher order function for ClickableSpan Kotlin:高阶 Function:sortedWith() - Kotlin: Higher Order Function : sortedWith() 无法为child()中的参数“ pathString”传递null - Can't pass null for argument 'pathString' in child() onClickListner无法传递“ this”参数错误 - Can't get pass “this” argument Error with the onClickListner 无法使用自定义适配器从 firebase 获取数据,“Сan't pass null for argument pathString in child ()” - Can't get data from firebase using custom adapter, “Сan't pass null for argument pathString in child ()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM