什么是“特定类型的任意对象”。在java 8中意味着什么? [英] What does "an Arbitrary Object of a Particular Type" mean in java 8?
问题描述
在Java 8中,有方法参考功能。
其中一种是引用特定类型的任意对象的实例方法
In Java 8 there is "Method Reference" feature. One of its kind is "Reference to an instance method of an arbitrary object of a particular type"
http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html#type
有人可以解释特定类型的任意对象在该上下文中的含义吗?
Can someone explain what does "arbitrary object of particular type" mean in that context ?
推荐答案
它是对某种类型的实例方法的引用。在示例的情况下, compareToIgnoreCase
是来自 String
的方法。程序知道它可以在 String
的实例上调用此方法,因此它可以获取该类型的引用和任何对象,并保证该方法存在。
It is a reference to an instance method from some type. In the case of the example, compareToIgnoreCase
is a method from String
. The program knows that it can invoke this method on an instance of String
, so it can take the reference and any object of that type and be guaranteed the method exists.
我会将它与方法
类进行比较,因为它们引用了一个方法,可以在一些任意实例上调用类型。
I would compare this to the Method
class in that they refer to a method and can be invoked on an arbitrary instance of some type.
例如,它可以使用两个 String
对象并调用 compareToIgnoreCase
on one并使用另一个作为参数来匹配方法签名。这允许它根据数组类型的任何方法获取数组并对其进行排序,而不是要求比较器实例执行此操作。
For the example, it can use two String
objects and call compareToIgnoreCase
on one and use the other as an argument to match the method signature. This allows it to take the array and sort it based on any method of the array type instead of requiring a comparator instance to do this instead.
以下是示例任何没有点击问题链接的人:
And here is the example for anyone who didn't click on the link in the question:
String[] stringArray = { "Barbara", "James", "Mary", "John",
"Patricia", "Robert", "Michael", "Linda", "George" };
Arrays.sort(stringArray, String::compareToIgnoreCase);
这篇关于什么是“特定类型的任意对象”。在java 8中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!