如何提供一个对象以及对那个class对象的方法引用?
How to provide an object as well as a method reference to an object of that class?
我编写了一个为控制台创建输入菜单的方法,该菜单显示多个选项供用户选择,returns 是用户选择的选项。该方法接受一个对象数组,并将通过对它们调用 toString()
方法来显示它们。问题是,在某些情况下,我不想在这些对象上调用 toString()
方法,但可能会调用 getName()
方法。因此我想让传递一个方法引用成为可能,它可以在对象上调用并且将 return 和 String
。
然后我可以f.e。传递一组人员和 getFullName()
方法。这些人会在控制台上显示他们的全名,但我仍然会得到 person 对象 returned,我不必通过全名找到 person 对象。
这是我当前的方法代码:
/**
* Prints the provided question and the options to choose from
*
* @param question
* the question to ask the user
* @param options
* list of objects the user can choose from
* @return chosen object
*/
public Object getMultipleChoiceResult(String question, List<?> options) {
int result = 0;
while (result > options.size() | result < 1) {
System.out.println(question);
for (int i = 1; i <= options.size(); i++) {
System.out.println("(" + i + ") " + options.get(i - 1).toString());
}
try{
result = scanner.nextInt();
} catch (InputMismatchException e) {
System.err.println("wrong input");
scanner.next();
}
}
return options.get(result - 1);
}
你明白我在找什么吗?有可能吗?
有很多方法可以实现这一点。一种相对现代的方法是传递 method reference:
private static <T> void showList(List<T> list, Function<T,String> f) {
for (T t : list) {
System.out.println(f.apply(t));
}
}
对该方法的调用如下所示:
showList(myList, MyType::getFullName);
以上假定 myList
是一个 List<MyType>
,并且 MyType
有一个非静态方法 getFullName()
返回一个 String
。
我想你正在寻找这个:
public <T> T getMultipleChoiceResult(String question, List<T> options, Function<T, String> toString) {
// ...
System.out.println("(" + i + ") " + toString.apply(options.get(i - 1)));
// ...
}
在你的例子中,你可以这样称呼它:
Object result = getMultipleChoiceResult(question, options, Object::toString);
或者您可以传递 Person
的列表并打印 Person.getFullName()
:
Person result = getMultipleChoiceResult(question, persons, Person::getFullName);
我编写了一个为控制台创建输入菜单的方法,该菜单显示多个选项供用户选择,returns 是用户选择的选项。该方法接受一个对象数组,并将通过对它们调用 toString()
方法来显示它们。问题是,在某些情况下,我不想在这些对象上调用 toString()
方法,但可能会调用 getName()
方法。因此我想让传递一个方法引用成为可能,它可以在对象上调用并且将 return 和 String
。
然后我可以f.e。传递一组人员和 getFullName()
方法。这些人会在控制台上显示他们的全名,但我仍然会得到 person 对象 returned,我不必通过全名找到 person 对象。
这是我当前的方法代码:
/**
* Prints the provided question and the options to choose from
*
* @param question
* the question to ask the user
* @param options
* list of objects the user can choose from
* @return chosen object
*/
public Object getMultipleChoiceResult(String question, List<?> options) {
int result = 0;
while (result > options.size() | result < 1) {
System.out.println(question);
for (int i = 1; i <= options.size(); i++) {
System.out.println("(" + i + ") " + options.get(i - 1).toString());
}
try{
result = scanner.nextInt();
} catch (InputMismatchException e) {
System.err.println("wrong input");
scanner.next();
}
}
return options.get(result - 1);
}
你明白我在找什么吗?有可能吗?
有很多方法可以实现这一点。一种相对现代的方法是传递 method reference:
private static <T> void showList(List<T> list, Function<T,String> f) {
for (T t : list) {
System.out.println(f.apply(t));
}
}
对该方法的调用如下所示:
showList(myList, MyType::getFullName);
以上假定 myList
是一个 List<MyType>
,并且 MyType
有一个非静态方法 getFullName()
返回一个 String
。
我想你正在寻找这个:
public <T> T getMultipleChoiceResult(String question, List<T> options, Function<T, String> toString) {
// ...
System.out.println("(" + i + ") " + toString.apply(options.get(i - 1)));
// ...
}
在你的例子中,你可以这样称呼它:
Object result = getMultipleChoiceResult(question, options, Object::toString);
或者您可以传递 Person
的列表并打印 Person.getFullName()
:
Person result = getMultipleChoiceResult(question, persons, Person::getFullName);