接口组成更新
方法引用符::
lambda表达式支持的方法引用
常见的引用方式:
引用类方法
引用对象的实例方法
引用类的实例对象
引用构造器
引用类方法
格式:类名::静态方法
范例:Integer::parseInt
Integer类的方法:public static int parseInt(String s)将此s转化为Int类型数据;
Lambda表达式被引用方法替代的时候,它的形式参数全部传递给静态方法作为参数
代码实例
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);
}
}
定义接口
public interface yinyong {
int cover(String s);
}
引用对象的实例方法
格式:对象::成员方法
范例:“HelloWorld”::toUpperCase
代码实例:
主程序
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");
}
}
接口b1:
public interface zhuandaxie {
void b1(String s);
}
引用类a1:
public class zhuanhuan {
public void a1(String a){
String result = a.toUpperCase();
System.out.println(result);
}
}
引用类的实例方法
格式:类名:成员方法
范例:String::substring
String类中的方法:Public String substring(Int beginIndex,Int endIndex)
从beginIndex开始到endIndex结束,截取字符串,返回一个子串,子串的长度是beginIndex-endIndex
引用构造器
格式:类名::new