2017-01-03 5 views
0

するための方法で関数インタフェースを使用した:私はこのコードを持っている1行を持つ一般的な戻り値の型

public class FunctionTest { 

    public Double getDouble(Function<Integer, Double> function) { 
     function = t -> Double.valueOf(t); 
     return function.apply(???); //not sure how to implement this 
    } 

    public Function<Integer, Double> getFunction(Integer t) { 
     return r -> Double.valueOf(t); // not able to fetch value from this as the return type is Function 
    } 

    public static void main(String[] args) { 

     Function<Integer, Double> f = t -> Double.valueOf(t); // works fine 
     System.out.println(f.apply(4)); // how to aechive this using method? 
     System.out.println(f); 
    } 

} 

あなたが組み合わせf.apply(4)と罰金t -> Double.valueOf(t)作品を見れば、今の質問は、私が試した同じのための方法を設計する方法であります2つの方法が失敗しました。

私を助けてください。

答えて

0

)私は、これを行う方法を考え出した、シンプルだが確かではなかった、それは

public Function<Integer, Double> getFunction() { 
     return r -> Double.valueOf(r); 
    } 

、その後

object.getFunction().apply(integer) 
+2

効果的であるならば、あなたの 'のgetFunctionを(簡素化することができます申し訳ありませんが、' 'には、 Double :: valueOf'を返す – Ash

+0

ありがとうございます非常に便利です:) – user7036414

関連する問題