“特定类型的任意对象"是什么意思?在 Java 8 中是什么意思? [英] What does "an Arbitrary Object of a Particular Type" mean in java 8?

查看:33
本文介绍了“特定类型的任意对象"是什么意思?在 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.

我会将其与 Method 类进行比较,因为它们引用一个方法并且可以在某种类型的任意实例上调用.

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 并使用另一个作为参数来匹配方法签名.这允许它根据数组类型的任何方法获取数组并对其进行排序,而不是需要比较器实例来执行此操作.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆