java 8 中的“特定类型的任意对象”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23533345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:39:22  来源:igfitidea点击:

What does "an Arbitrary Object of a Particular Type" mean in java 8?

javajava-8method-reference

提问by Roman Ivanov

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"

在 Java 8 中有“方法引用”功能。其中一种是“引用特定类型的任意对象的实例方法”

http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html#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 ?

有人可以解释在这种情况下“特定类型的任意对象”是什么意思?

采纳答案by Andrew Vitkus

It is a reference to an instance method from some type. In the case of the example, compareToIgnoreCaseis 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.

它是对某种类型的实例方法的引用。在示例的情况下,compareToIgnoreCase是来自 的方法String。程序知道它可以在 的实例上调用此方法String,因此它可以获取引用和该类型的任何对象,并保证该方法存在。

I would compare this to the Methodclass in that they refer to a method and can be invoked on an arbitrary instance of some type.

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

For the example, it can use two Stringobjects and call compareToIgnoreCaseon 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.

例如,它可以使用两个String对象并调用compareToIgnoreCase一个并将另一个用作参数来匹配方法签名。这允许它根据数组类型的任何方法获取数组并对其进行排序,而不是需要比较器实例来执行此操作。

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);

回答by Jawad

The example given from the Oracle Doc linked is:

从 Oracle Doc 链接给出的例子是:

String[] stringArray = { "Barbara", "James", "Mary", "John", "Patricia", "Robert", "Michael", "Linda" };
Arrays.sort(stringArray, String::compareToIgnoreCase);

The lambda equivalent of

lambda 等价于

 String::compareToIgnoreCase

would be

将是

(String a, String b) -> a.compareToIgnoreCase(b)

The Arrays.sort()method is looking for a comparator as its second argument (in this example). Passing String::compareToIgnoreCasecreates a comparator with a.compareToIgnoreCase(b)as the compare method's body. You then ask well what's aand b. The first argument for the compare method becomes aand the second b. Those are the arbitrary objects, of the type String (the particular type).

Arrays.sort()方法正在寻找一个比较器作为它的第二个参数(在这个例子中)。传递String::compareToIgnoreCase创建一个比较器a.compareToIgnoreCase(b)作为比较方法的主体。然后你很好地问什么是ab。compare 方法的第一个参数变为a,第二个参数变为b. 这些是 String 类型(特定类型)的任意对象。

Don't understand?

不明白?

  • Make sure you know what a comparator is and how to implement it.
  • Know what a functional interfaceis and how it affects lambdas in Java.
  • A comparator is a functional interfacethat's why the method reference becomes the body of the compare method inside the comparator object.
  • Read the source below for another exampleat the bottom of the page.
  • 确保您知道比较器是什么以及如何实现它
  • 了解函数式接口是什么以及它如何影响 Java 中的 lambdas。
  • 比较器是一个功能接口,这就是为什么该方法引用变得比较对象内的比较方法的正文中。
  • 阅读下面来源以获得页面底部的另一个示例

Read more at the source: http://moandjiezana.com/blog/2014/understanding-method-references/

在来源阅读更多信息:http: //moandjiezana.com/blog/2014/understanding-method-references/

回答by Raj Gopal Bhallamudi

In this case there is an array of objects of a particular type(String) and any random object in array can call its instance method . This approach allows a class to refer to its instance method as if it is a static method .

在这种情况下,有一个特定类型(字符串)的对象数组,数组中的任何随机对象都可以调用其实例方法。这种方法允许一个类引用它的实例方法,就好像它是一个静态方法一样。

Also this approach works only for built in class of java like String but not for user defined class . In case of user defined class the instance method can only be referred by its object .

此外,这种方法仅适用于像 String 这样的内置 java 类,但不适用于用户定义的类。在用户定义类的情况下,实例方法只能由其对象引用。

回答by Viraj

Please see the below code sample which explains "Reference to an Instance Method of an Arbitrary Object of a Particular Type" category described in https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

请参阅以下代码示例,该示例解释了https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html中描述的“对特定类型的任意对象的实例方法的引用”类别

import java.util.Arrays;

class Person{
String name;

//constructor
public Person(String name){
    this.name = name;
}

//instance method 1
public int personInstanceMethod1(Person person){
    return this.name.compareTo(person.name);
}

//instance method 2
public int personInstanceMethod2(Person person1, Person person2){
    return person1.name.compareTo(person2.name);
}
}

class Test {
public static void main (String[] args) throws Exception{
    Person[] personArray = {new Person("A"), new Person("B")};

    // Scenario 1 : Getting compiled successfully
    Arrays.sort(personArray, Person::personInstanceMethod1);

    // Scenario 2 : Compile failure
    Arrays.sort(personArray, Person::personInstanceMethod2);

    // Scenario 3 : Getting compiled successfully. 
    Person personInstance = new Person("C");
    Arrays.sort(personArray, personInstance::personInstanceMethod2);

    // Scenario 4 : Getting compiled successfully. As the same way as "Scenario 1"
    String[] stringArray = { "Barbara", "James", "Mary", "John",
            "Patricia", "Robert", "Michael", "Linda" };
    Arrays.sort(stringArray, String::compareToIgnoreCase);
}

}

Scenario 1 and Scenario 4 describes "Reference to an Instance Method of an Arbitrary Object of a Particular Type" category described in https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

场景 1 和场景 4 描述了https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html中描述的“对特定类型的任意对象的实例方法的引用”类别

If the method parameter takes a variable in same instance Type as the instance Type of the element, you can call that instance method using Type.(Person::personInstanceMethod1)

如果方法参数采用与元素的实例 Type 相同的实例 Type 中的变量,则可以使用 Type.(Person::personInstanceMethod1) 调用该实例方法

Compare "personInstanceMethod1" instance method in "Person" class with "compareToIgnoreCase" instance method in "String" class (https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-) to see the similarity. Both are taking a single parameter with the same Type.

比较“Person”类中的“personInstanceMethod1”实例方法和“String”类中的“compareToIgnoreCase”实例方法(https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareToIgnoreCase -java.lang.String-) 来查看相似度。两者都采用相同类型的单个参数。

Compare Scenario 1 and Scenario 2 to see the difference.

比较方案 1 和方案 2 以查看差异。

回答by Laks

Let me put it another way. So, if your lambda expression looks like the following:

让我换一种说法。因此,如果您的 lambda 表达式如下所示:

(<ContainingType arg>, <otherArgs>) -> arg.instanceMethod(<otherArgs>)

It can be replaced with method reference such as

它可以替换为方法引用,例如

ContainingType::instanceMethod

So, for the lambda expression

所以,对于 lambda 表达式

(String a, String b) -> a.compareToIgnoreCase(b)

It can be replaced with method reference such as

它可以替换为方法引用,例如

String::compareToIgnoreCase

Here, the particular type is the ContainingType which is String. And an instance of it (String) is arbitrarysince we haven't declared or initialized it yet and these are simply the arguments here. And hence "arbitrary object of particular type" in this context is "arbitrary object of String type"

在这里,特定类型是 ContainingType ,它是 String它的一个实例(字符串)是任意的,因为我们还没有声明或初始化它,这些只是这里的参数。因此,“特定类型的任意对象”在此上下文中是“字符串类型的任意对象