gpt4 book ai didi

JAVA 8 对实例方法的引用。 vals[i] 如何自动映射到 "InstanceMethWithObjectRefDemo.counter"中的 this

转载 作者:搜寻专家 更新时间:2023-11-01 03:18:17 24 4
gpt4 key购买 nike

我正在阅读一本 java 书籍,我得到了这段代码。我知道方法引用是如何制作的,但这让我很头疼。我不知道 if(f.func(vals[i], v)) 中的 vals[i] 如何充当映射的 this功能。

// Use an instance method reference with different objects. 
// A functional interface that takes two reference arguments
// and returns a boolean result.
interface MyFunc<T> {
boolean func(T v1, T v2);
}
// A class that stores the temperature high for a day.
class HighTemp {
private int hTemp;
HighTemp(int ht) {
hTemp = ht;
}

// Return true if the invoking HighTemp object has the same
// temperature as ht2.
boolean sameTemp(HighTemp ht2) {
return hTemp == ht2.hTemp;
}
// Return true if the invoking HighTemp object has a temperature
// that is less than ht2.
boolean lessThanTemp(HighTemp ht2) {
return hTemp < ht2.hTemp;
}
}
class InstanceMethWithObjectRefDemo {
// A method that returns the number of occurences
// of an object for which some criteria, as specified by
// the MyFunc parameter, is true.
static <T> int counter(T[] vals, MyFunc<T> f, T v) {
int count = 0;
for(int i=0; i < vals.length; i++) {
if(f.func(vals[i], v)) count++;
}
return count;
}

public static void main(String args[]) {
int count;
// Create an array of HighTemp objects.
HighTemp[] weekDayHighs = { new HighTemp(89), new HighTemp(82),
new HighTemp(90), new HighTemp(89),
new HighTemp(89), new HighTemp(91),
new HighTemp(84), new HighTemp(83) };

// Use counter() with arrays of the class HighTemp.
// Notice that a reference to the instance method
// sameTemp() is passed as the second argument.
count = counter(weekDayHighs, HighTemp::sameTemp,new HighTemp(89));
System.out.println(count + " days had a high of 89");

// Now, create and use another array of HighTemp objects.
HighTemp[] weekDayHighs2 = { new HighTemp(32), new HighTemp(12),
new HighTemp(24), new HighTemp(19),
new HighTemp(18), new HighTemp(12),
new HighTemp(-1), new HighTemp(13) };

count = counter(weekDayHighs2, HighTemp::sameTemp,new HighTemp(12));
System.out.println(count + " days had a high of 12");
// Now, use lessThanTemp() to find days when temperature was less
// that a specified value.
count = counter(weekDayHighs, HighTemp::lessThanTemp,new HighTemp(89));
System.out.println(count + " days had a high less than 89");
count = counter(weekDayHighs2, HighTemp::lessThanTemp,new HighTemp(19));
System.out.println(count + " days had a high of less than 19");
}
}

最佳答案

在此上下文中,HighTemp::sameTemp 等同于 (t1, t2) -> t1.sameTemp(t2)

documentation它被称为对特定类型的任意对象的实例方法的引用

The following is an example of a reference to an instance method of an arbitrary object of a particular type:

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

The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.compareToIgnoreCase(b).

关于JAVA 8 对实例方法的引用。 vals[i] 如何自动映射到 "InstanceMethWithObjectRefDemo.counter"中的 this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40026009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com