2016-10-13 3 views
4

私はこのコードを持っているからJavaの本を読んでいます。メソッドリファレンスがどのように作られているのか知っていますが、これは私に頭痛を与えています。私はif(f.func(vals[i], v))vals[i]がマップされた機能のためにthisとしてどのように動作するのか分かりません。この文脈HighTemp::sameTempJAVA 8インスタンスメソッドへの参照。どのようにvals [i]が自動的にこの "InstanceMethWithObjectRefDemo.counter"にマップされます

// 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"); 
    } 
} 
+0

'new HighTemp(89)'を見てください。 – chrylis

答えて

1

(t1, t2) -> t1.sameTemp(t2)と同等です。それは、以下の特定のタイプ

の任意のオブジェクトのインスタンスメソッドへ基準として知られているdocumentation

は、任意のオブジェクトのインスタンスメソッドへの参照の一例です特定の型:

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

メソッドリファレンスString :: compareToIgnoreCaseの同等のラムダ式は、仮引数リスト(String a、String b)を持ちます。ここで、aとbは、この例をよりよく説明するために使用される任意の名前です。メソッドリファレンスは、メソッドa.compareToIgnoreCase(b)を呼び出します。

+1

修正。このコンテキストでは、HighTemp :: sameTempは(t1、t2) - > t1.sameTemp(t2) –

+0

@LalitRao Oups!と同等です。修正しました、ありがとうございます。 – Spotted

関連する問題