Referencia del Método estático - pequeño ejemplo de programa de asesoramiento

nativo:

Estoy atascado con este programa a continuación en relación con una cosa y es ... ¿Cómo se puede valorar "17" llegar a esPrimo método cuando en el método numTest está separada por comas y no puedo encontrar ninguna transferencia de este valor "17" a ¿este método? Gracias por ayudarnos a mover más. ¿Alguien puede explicar mi PLS movimiento del valor "17"?

// Demonstrate a method reference for a static method. 

// A functional interface for numeric predicates that operate 
// on integer values. 
interface IntPredicate { 
  boolean test(int n); 
} 

// This class defines three static methods that check an integer 
// against some condition. 
class MyIntPredicates { 
  // A static method that returns true if a number is prime. 
  static boolean isPrime(int n) { 

    if(n < 2) return false; 

    for(int i=2; i <= n/i; i++) { 
      if((n % i) == 0)  
        return false; 
    } 
    return true; 
  } 

  // A static method that returns true if a number is even. 
  static boolean isEven(int n) { 
    return (n % 2) == 0; 
  } 

  // A static method that returns true if a number is positive. 
  static boolean isPositive(int n) { 
    return n > 0; 
  } 
}     

class MethodRefDemo { 

  // This method has a functional interface as the type of its 
  // first parameter. Thus, it can be passed a reference to any 
  // instance of that interface, including one created by a 
  // method reference. 
  static boolean numTest(IntPredicate p, int v) { 
    return p.test(v); 
  } 

  public static void main(String args[]) 
  { 
    boolean result; 

    // Here, a method reference to isPrime is passed to numTest(). 
    result = numTest(MyIntPredicates::isPrime, 17); 
    if(result) System.out.println("17 is prime."); 

    // Next, a method reference to isEven is used. 
    result = numTest(MyIntPredicates::isEven, 12); 
    if(result) System.out.println("12 is even.");  

    // Now, a method reference to isPositive is passed. 
    result = numTest(MyIntPredicates::isPositive, 11); 
    if(result) System.out.println("11 is positive."); 
  } 
}
Eran :

numTestacepta una IntPredicatey una int. Una IntPredicatees una interfaz funcional que tiene un único método que toma una inty devuelve una boolean.

MyIntPredicates::isPrimees un método de referencia que coincide con el IntPredicateinterfaz, y por lo tanto se puede pasar a numTest.

numTest(MyIntPredicates::isPrime, 17)invoca isPrime(17)llamando p.test(v).

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=189315&siteId=1
Recomendado
Clasificación