JAVA9 all to come out, JAVA8 new features are not clear, is not a bit behind OH ~
With the support of the new Lamda feature, you can use the LAMDA expression to create an anonymous method in JAVA8. However, sometimes we just need to invoke an existing method (as defined in Java), and at this point java8 the new feature "method reference" will further simplify the operation (note: LAMDA support is required).
Four types of method references:
- Reference static Method--class Name:: Static method name;
- A normal way to reference an instance of an object--and a sample object:: Common method;
- A common method of referencing an instance of any object of a type--specific class:: Common method;
- Reference construction Method--Class name:: New
Referencing static methods
For example: the ValueOf () method in the String class: public static String valueOf (int x);
1 /**2 * Reference interface for the implementation method3 * @param<P> parameter types for reference methods4 * @param<R> return type of a reference method5 */6 InterfaceMyinterface<p,r>{7 PublicR function (P p);//similar to string.valueof (int x)8 }9 Ten Interfacemyinterface1{ One String function (Integer a); A } - - Public classMain { the - Public Static voidTest (MyInterface1 myInterface1) { -String result = Myinterface1.function (2000); -System.out.println (result+ "--"); + } - + Public Static voidMain (string[] args) { A at //Anonymous inner class implementation -TestNewMyInterface1 () { - @Override - PublicString function (Integer a) { - returnstring.valueof (a); - } in }); - to //LAMDA Expression Implementation +Test ((a)string.valueof (a)); - the //method Reference implementation: A static method that references a class *myinterface<integer,string> msg =string::valueof; $String str = msg.function (2000);Panax Notoginseng System.out.println (str); - } the}
Common methods for referencing an instance of an object
For example: the toUpperCase () method in the String class: public string toUpperCase ();
This method has no parameters, but it has a return value, and this method must be called only when there is an instantiated object.
1 InterfaceMyinterface2<r>{2 PublicR Upper ();3 }4 Public classMain1 {5 Public Static voidMain (string[] args) {6String str =NewString ("Hello");7myinterface2<string> msg =str::touppercase;8System.out.println (Msg.upper ());//calling the upper method is equivalent to calling the toUpperCase method9 }Ten}
It has been found in the previous demo that if you want to implement a reference to a method, you must have an interface, and there can only be one method in the interface, otherwise the method cannot be referenced.
Therefore, in order to ensure that the referenced interface can only define a method, it is necessary to restrict the interface, using @functionalinterface annotations.
1 @FunctionalInterface // This is a functional interface, and only one method can be defined 2 Interface Myinterface2<r>{3public R Upper (); 4 }
Common method for referencing an instance of an arbitrary object of a type
For example: CompareTo (String str1,string str2) method in the String class public int compareTo (string anotherstring);
If you want to compare, compare the form: String 1 object. CompareTo (String 2 object), to prepare two parameters.
1@FunctionalInterface//This is a functional interface, and only one method can be defined2 InterfaceMyinterface3<p>{3 Public intCompare (P p1,p p2);4 }5 Public classMain2 {6 Public Static voidMain (string[] args) {7myinterface3<string> msg =String::compareto;8System.out.println (Msg.compare ("A", "B"));9 }Ten}
Instead of defining the object before the method reference, it can be understood that the object is defined in the parameter.
Reference Construction method
1@FunctionalInterface//This is a functional interface, and only one method can be defined2 InterfaceMyinterface4<c>{3 PublicC person (String N,inta);4 }5 classperson{6 PrivateString name;7 Private intAge ;8 9 PublicPerson (String name,intAge ) {Ten This. Name =name; One This. Age =Age ; A } - @Override - PublicString toString () { the return"Name:" + This. name+ ", Age:" + This. Age; - } - } - Public classMain3 { + Public Static voidMain (string[] args) { -myinterface4<person> msg = Person::New;//Reference Construction Method +Person person = Msg.person ("Xiaoming", 20); A System.out.println (person); at } -}
Summary: A little ruin the feeling of the three views!
JAVA8 new Feature--method reference