引用特定对象的实例方法 [英] Reference to an instance method of a particular object
问题描述
在下面的代码中,它在使用类名传递方法引用变量时有效,但在使用用户对象传递引用变量时会出错。
In the following code, it works when passing the method reference variable with the class name, but when passing the reference variable with a user object there is an error.
public class User {
private String name;
public User(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
User u1 = new User("AAA");
User u2 = new User("BBB");
User u3 = new User("ZZZ");
List<User> userList = Arrays.asList(u1, u2, u3);
userList.forEach(User::printName); // works
userList.forEach(u1::printName); // compile error
}
}
推荐答案
userList.forEach
期待消费者<?扩展用户>
- 换句话说,这是一种接受用户
引用的方法并使用它做一些事情。
userList.forEach
is expecting a Consumer<? extends User>
- in other words, a method which accept a User
reference and do something with it.
可能是:
-
接受
用户的静态方法
参数,在这种情况下,参数将在每次迭代时使用列表中的相关元素填充:
A static method accepting a
User
parameter, in which case the parameter will be populated with the relevant element in the list on each iteration:
staticMethod(userFromList)
接受用户
参数,提供特定的实例来调用它 - 再次,该参数将填充相关元素:
An instance method (of any class) accepting a User
parameter, provided with a specific instance to call it on - again, the parameter will be populated with the relevant element:
target.instanceMethod(userFromList)
带 no 参数的用户
上的实例方法,提供不带特定实例,其中方法调用的target 将是每次迭代时列表中的相关元素:
An instance method on User
with no parameters, provided without a specific instance, which case the target of the method call will be the relevant element in the list on each iteration:
userFromList.instanceMethod()
因为你已经试图指定一个目标和该方法没有任何参数, forEach
方法对每个元素没有任何作用 - 它可以' t将它作为参数传递,因为该方法没有任何参数,并且它不能将它用作方法目标,因为您已经指定了一个。
Because you've tried to specify a target and the method doesn't have any parameters, the forEach
method has nothing it can do with each element - it can't pass it as an argument because the method doesn't have any parameters, and it can't use it as the method target because you've already specified one.
您的工作代码显示了第三个示例。以下是另外两种方法可以让您演示前两个:
Your working code shows the third example. Here are two other methods to allow you to demonstrate the first two:
public class UserPrinter {
private final String name;
public UserPrinter(String name) {
this.name;
}
public static void staticPrintUser(User user) {
// Assuming you add a User.getName() method
System.out.println("staticPrintUser: " + user.getName());
}
public void instancePrintUser(User user) {
System.out.println("instancePrintUser (instance " + name + "): "
+ user.getName());
}
}
然后:
userList.forEach(UserPrinter::staticPrintUser); // equivalent to
//userList.forEach(p -> UserPrinter.staticPrintUser(p));
UserPrinter printer = new UserPrinter("my printer");
userList.forEach(printer::instancePrintUser); // equivalent to
//userList.forEach(p -> printer.instancePrintUser(p));
如果确实想要致电 printUser
在同一个用户
上三次,忽略列表中的用户
,你可以使用:
If you really want to call printUser
on the same User
three times, ignoring the User
in the list, you could use:
userList.forEach(ignored -> u1.printName());
这篇关于引用特定对象的实例方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!