Interface Composition Update
method reference::
Method references supported by lambda expressions
Common reference methods:
referencing class methods,
referencing instance methods of objects,
referencing instance objects of classes,
referencing constructors
reference class method
Format: class name::static method
Example: Integer::parseInt
Integer class method: public static int parseInt(String s) converts this s into Int type data;
when a Lambda expression is replaced by a reference method, its formal parameters All passed to static methods as arguments
code example
public class yinyongdemo {
public static void main(String[] args) {
yin(s -> Integer.parseInt(s));
yin(Integer::parseInt);//引用方法替代lambda表达式
}
private static void yin(yinyong y){
int co = y.cover("788");
System.out.println(co);
}
}
define interface
public interface yinyong {
int cover(String s);
}
Instance method of reference object
Format: Object::Member Method
Example: "HelloWorld"::toUpperCase
Code Example:
Main Program
public class zhuandaxiedemo {
public static void main(String[] args) {
//Lambda方法
c1(s ->System.out.println(s.toUpperCase()) );
//引用方法
zhuanhuan zh = new zhuanhuan();
c1(zh::a1 );
}
private static void c1 (zhuandaxie z){
z.b1("jjjddd");
}
}
Interface b1:
public interface zhuandaxie {
void b1(String s);
}
Reference class a1:
public class zhuanhuan {
public void a1(String a){
String result = a.toUpperCase();
System.out.println(result);
}
}
Instance method of reference class
Format: class name: member method
Example: String::substring
Method in the String class: Public String substring(Int beginIndex, Int endIndex)
starts from beginIndex and ends at endIndex, intercepts the string, returns a substring, and the length of the substring is beginIndex-endIndex
reference constructor
Format: class name::new