2017-02-14 2 views
2

Wie werden die beiden Fehlerlinien in Klasse Test ohne Änderung an Klasse Case gelöst?Wie behebe ich mehrdeutige Methodenreferenz?

class Case { 
    public boolean isEmpty() { 
     return true; 
    } 

    public static boolean isEmpty(Case t) { 
     return true; 
    } 
} 

public class Test { 
    public static void main(String[] args) { 
     Predicate<Case> forNonStaticMethod = Case::isEmpty;//<--TODO: fix compile error: 
     Predicate<Case> forStaticMethod = Case::isEmpty;//<--TODO: fix compile error: 
     //Ambiguous method reference: both isEmpty() and isEmpty(Case) from the type Case are eligible   
    } 
} 

Antwort

4

Sie Lambda-Ausdrücke anstelle von Methoden Referenzen verwenden:

Predicate<Case> forNonStaticMethod = c -> c.isEmpty(); 
Predicate<Case> forStaticMethod = c -> Case.isEmpty(c); 
Verwandte Themen